Im migrating a service from a normal domain DNS server to a IP only server and this provides a json service for my app, the problem is I cant retrieve the JSONArray with the following code in the new URL:
protected JSONArray doInBackground(String... arg0) {
String reponse;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse responce = httpclient.execute(httppost);
HttpEntity httpEntity = responce.getEntity();
reponse = EntityUtils.toString(httpEntity);
return new JSONArray(reponse);
//With the oldURL I printed the JSON as a string and everithing OK but the new URL returns a null string of JSON
}catch(Exception e){
e.printStackTrace();
}
return null;
}
String newurlexample = "http://111.22.333.44:1234/FOLD/blablabla";
String oldurl = "https:/example.com/FOLD/file.json";
And I get the following LOG:
07-13 17:47:02.142: W/System.err(18824): org.json.JSONException: Value Method of type java.lang.String cannot be converted to JSONArray
07-13 17:47:02.145: W/System.err(18824): at org.json.JSON.typeMismatch(JSON.java:111)
07-13 17:47:02.145: W/System.err(18824): at org.json.JSONArray.<init>(JSONArray.java:96)
07-13 17:47:02.146: W/System.err(18824): at org.json.JSONArray.<init>(JSONArray.java:108)
07-13 17:47:02.146: W/System.err(18824): at com.karlol.***.Doctor_Fragment$GetData.doInBackground(Doctor_Fragment.java:171)
07-13 17:47:02.146: W/System.err(18824): at com.karlol.***.Doctor_Fragment$GetData.doInBackground(Doctor_Fragment.java:1)
07-13 17:47:02.147: W/System.err(18824): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-13 17:47:02.147: W/System.err(18824): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-13 17:47:02.147: W/System.err(18824): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-13 17:47:02.147: W/System.err(18824): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
From the title of your question I can notice that there is something wrong between the URL you were using and the new ip .. first you need to make sure your new web-service provide the same result as the old one .. any way you can try to retrieve your data using this :
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse responce = httpclient.execute(httppost);
HttpEntity httpEntity = responce.getEntity();
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
reponse = sb.toString();
return new JSONArray(reponse);
//With the oldURL I printed the JSON as a string and everithing OK but the new URL returns a null string of JSON
}catch(Exception e){
e.printStackTrace();
}
EDIT:
If you use shared hosting server.. it is not uncommon to have multiple web sites on the same IP, distinguished only by the site name (so-called virtual hosting). What you are doing will only work for the case where there is a single site on a given IP.
EDIT 2
You need to test your webservice using a RESTful plugin (chrome or firefox) i.e., Advanced rest client
We have the same problem before try to use this. change the one inside your try and catch with this. hope it helps.
FileCache filecache;
String result="";
HttpURLConnection conn = null;
String finalurl="http://111.22.333.44:1234/FOLD/blablabla";
filecache = new FileCache(context);
File f = filecache.getFile(finalurl);
try {
URL url = new URL(finalurl);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
FileReader isr = new FileReader(f);
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
is.close();
os.close();
conn.disconnect();
return new JSONArray(result);
}catch (Exception ex) {
Log.e("Error", ex+"can't access" + finalurl + result);
}
FileCache.java
import android.content.Context;
import java.io.File;
/**
* Created by cristiana214 on 1/26/2015.
*/
public class FileCache {
private File cacheDir;
public FileCache(Context context) {
// Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
cacheDir = new File(context.getExternalCacheDir(),"folder/i");
} else
cacheDir = context.getCacheDir();
if (!cacheDir.exists())
cacheDir.mkdirs();
}
public File getFile(String url) {
String filename = String.valueOf(url.hashCode());
File f = new File(cacheDir, filename);
return f;
}
public void clear() {
File[] files = cacheDir.listFiles();
if (files == null)
return;
for (File f : files)
f.delete();
}
}
Utils.java
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by cristiana214 on 1/26/2015.
*/
public class Utils {
public static void CopyStream(InputStream is, OutputStream os){
final int buffer_size=1024*10;
try{
byte[] bytes=new byte[buffer_size];
for(;;){
int count=is.read(bytes, 0, buffer_size);
if(count==-1)
break;
os.write(bytes, 0, count);
}
}
catch(Exception ex){}
}
}
According to your error code
org.json.JSONException: Value Method of type java.lang.String cannot be converted to JSONArray
you are trying to convert a String to JSONArray. there is also a pointer to doInBackground task, line 171 so you should definitely check this line. I guess the problem is around these lines:
reponse = EntityUtils.toString(httpEntity);
return new JSONArray(reponse);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With