I can not get JSONArray from the IP server, but can I from a regular server?

I am transferring a service from a normal domain DNS server to an IP only server, and this provides a json service for my application, the problem is that I cannot get a 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) 
+7
json android
source share
5 answers

From the title of your question, I can see that there is something wrong between the URL you used and the new ip address. First you need to make sure that your new web service provides the same result as the old one. in any way that you can try to extract 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 a public hosting server, there are often several websites on the same IP address, which differs only in the name of the site (the so-called shared hosting). What you do will work only if there is one site on this IP address.

EDIT 2

You need to test your web service using the RESTful plugin (chrome or firefox), i.e. Advanced rest client

+2
source share

We have the same problem before trying to use this. change one inside your attempt and catch with it. 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){} } } 
+2
source share

According to the 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 a JSONArray. there is also a pointer to the doInBackground task, line 171, so you should definitely check this line. I think the problem is in these lines:

 reponse = EntityUtils.toString(httpEntity); return new JSONArray(reponse); 
+2
source share

If this helps someone, I had to change the request to GET instead of POST, and this code worked fine, although I could not get the POST request working.

  try { URL obj = new URL("http://anIPaddress/REST/Folder/Consult?Operation=AnOperation"); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); //con default method is GET con.setRequestProperty("Accept", "application/json"); con.setRequestProperty("Content-Type", "application/json"); int responseCode = con.getResponseCode(); Log.v("Lolo", "\nSending 'GET' request to URL : " + con.getURL()); Log.v("Lolo", "RESPONSE CODE : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return new JSONArray(response.toString()); } 

Thanks so much for your answers and your help.

+2
source share

Check this line in the log message:

W / System.err (18824): org.json.JSONException: value A method of type java.lang.String cannot be converted to a JSONArray

You get this exception because the response from the server you receive is not formatted as JSON format. Therefore, when you try to convert a response string to a JSONArray, it throws the above JSON exception.

Now the solution for your problem: you need to make sure the response is formatted as the correct JSON format. And write down your answer before parsing JSON, with which you can check the answer correctly or not.

Hope this helps you :) Thanks

+1
source share

All Articles