1

I'm running my API REST application from my mobile device and, apparently, it doesn't have any error because it doesn't close or kill the process automaticaly. It just run but without retrieving to me any information and it shows to me two erros in the log console.

Here is one of the problems that I saw in my log console: acAppName=/system/bin/surfaceflinger.

    06-04 16:49:21.527  17600-17600/com.example.user.project V/PhoneWindow﹕ DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{420a1478 com.example.user.project/com.example.user.project.MainActivity,ident = 0}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{4205a608 V.E..... R.....ID 0,0-0,0}
    06-04 16:49:21.527  17600-17600/com.example.user.project D/ActivityThread﹕ ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{4204b3c0 token=android.os.BinderProxy@4204aa68 {com.example.user.project/com.example.user.project.MainActivity}}
    06-04 16:49:21.590  17600-17600/com.example.user.project E/﹕ appName=com.example.user.project, acAppName=/system/bin/surfaceflinger
    06-04 16:49:21.590  17600-17600/com.example.user.project E/﹕ 0
    06-04 16:49:21.590  17600-17600/com.example.user.project E/﹕ appName=com.example.user.project, acAppName=/system/bin/surfaceflinger
    06-04 16:49:21.590  17600-17600/com.example.user.project E/﹕ 0
    06-04 16:49:21.592  17600-17600/com.example.user.project D/GraphicBuffer﹕ create handle(0x614af178) (w:480, h:800, f:1)
    06-04 16:49:21.593  17600-17600/com.example.user.project I/MaliEGL﹕ [Mali]window_type=1, is_framebuffer=0, errnum = 0

And some lines below I get this another problem:

06-04 17:32:24.340  23481-23503/com.example.user.project I/System.out﹕ >doSendRequest
06-04 17:32:24.341  23481-23503/com.example.user.project I/System.out﹕ <doSendRequest
06-04 17:32:24.382  23481-23503/com.example.user.project E/ServicioRest﹕ org.json.JSONException: Value {//Here the information retrieved of my JSON} of type org.json.JSONObject cannot be converted to JSONArray
06-04 17:32:24.399  23481-23481/com.example.user.project D/ListView﹕ measureHeightOfChildren adapter=com.com.example.user.project

I searched about both errors but for the first one I couldn't find anything that helped me but for the second one I found this: org.json.JSONObject cannot be converted to JSONArray in android

And I wanted to apply what RajaReddy PolamReddy said:

JSONObject object = new JSONObject(result);

   JSONArray Jarray = object.getJSONArray("contacts");

   for (int i = 0; i < Jarray.length(); i++) {
      JSONObject Jasonobject = Jarray.getJSONObject(i);

but I don't know how to apply it to my code:

 HttpClient httpClient = new DefaultHttpClient();

 HttpGet method = new HttpGet(url);

 method.setHeader("content-type", "application/json");

    try{
         HttpResponse response = httpClient.execute(method);
         String responseString = EntityUtils.toString(response.getEntity());
         JSONArray responseJSON = new JSONArray(responseString);
            for(int i=0; i<responseJSON.length(); i++){
                JSONObject object = responseJSON.getJSONObject(i);
            }
     }catch{
         Log.e("ServiceRest", ex.toString());       
     }

where url is the uri which access to my GET method of my API REST.

Note: I'm using my mobile device to access to the uri. Instead of localhost, because I'm in an external device, I'm using the IP network of my computer. I also tried with 10.0.2.2 and it doesn't work so please avoid answers that recommend me to use 10.0.2.2 as IP.

Note2: I have these permissions on my Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

Are both erros related? How can I solve them? Do you know why the first one appears?

Thanks in advance!

EDIT

What I retrieve of my JSON is (I simplified it, I put here the different types of data that it is retrieving):

int idMain = object.getInt("idMain");
String date = object.getString("date");
String name = object.getString("name");
double value = object.getDouble("value");

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date datePar = sdf.parse(date);
Community
  • 1
  • 1
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
  • 1
    What does your JSON look like? – Bidhan Jun 04 '15 at 16:09
  • What do you mean? How it retrieves to me the information in the browser? – Francisco Romero Jun 04 '15 at 16:11
  • 1
    What is the structure of the JSON that you're parsing? – Bidhan Jun 04 '15 at 16:13
  • @BidhanA I'm really new at this and I'm not secure if I understand in the correct way to you. Do you want what values it retrieves to me, that I'm retrieving from a database in phpmyadmin, or if they are `int`, `String` or whatever? – Francisco Romero Jun 04 '15 at 16:15
  • What do you get as responseString? It must be a json string right? – Bidhan Jun 04 '15 at 16:19
  • There are multiple values, there are columns that are `int`, another which are `String`, `double`, etc... – Francisco Romero Jun 04 '15 at 16:21
  • Post the responseString. If it is long, at least post a sample. – Bidhan Jun 04 '15 at 16:23
  • @BidhanA I'm goint to edit my question now about what I get of the object. – Francisco Romero Jun 04 '15 at 16:24
  • No I meant `String responseString = EntityUtils.toString(response.getEntity());` . When you execute this line, what is the value of responseString? It must be a string right? Post that string value.. – Bidhan Jun 04 '15 at 16:33
  • How can I see this String? I entered another question some hours before because I didn't understand very much how it works but anyone answered to me. It's this: http://stackoverflow.com/questions/30642237/what-exactly-returns-entityutils-tostringresponse – Francisco Romero Jun 04 '15 at 16:36
  • 1
    If the data returned by your server is wrapped in braces '{ ... }' then it is a JSONObject. If it's wrapped in brackets '[ ... ]' then it is a JSONArray. Trying to cast the former to the latter will throw the error you're getting. Meaning, you need to change it to 'JSONObject responseJSON = new JSONObject(responseString);' from which you can extract the JSONArray via r'esponseJSON.getJSONArray(String);' Check this out for a better explanation: http://stackoverflow.com/a/25002227/1426565 And to log your data, try logging with Log.d("sometag", responseString); – Cruceo Jun 04 '15 at 16:37
  • After you enter 'String responseString = EntityUtils.toString(response.getEntity());' try to Log it using ``Log.d("STRING", responseString)` . And by the way, if you don't know what is the string that you're getting, how are you even supposed to parse it? – Bidhan Jun 04 '15 at 16:38
  • @BidhanA Ok it retrieves the same information that I get in my browser when I put the url of my `GET` method. – Francisco Romero Jun 04 '15 at 16:58
  • @Guardanis According to your answer, the information that I was retrieving it's inside brackets like {} so I saw the database and I just had 1 row, so I think that to be an array it would be 2 rows, I add another one but it also retrieve me the same information than before, just the first row. Why it could be? I make a query `SELECT * FROM name_table`. – Francisco Romero Jun 04 '15 at 17:01

1 Answers1

1

The issue is with the casting. You are trying to retreive an object and cast to JsonArray where as it is JsonObject. The app doesnt crash because you surrond it with try/catch (btw: your code is missing the exception after the catch). If you post a sample of your json i could take a further look and help u solve it.

EE66
  • 4,601
  • 1
  • 17
  • 20
  • It is retrieving me the info like this: {"idMain":"1","date":"2015-06-23",....} and as @Guardanis said in the comments above, it is an Object. I think it was because I just had a row on the database. Now I add another one and it gives me the same information, just the first row. I made a query in my `GET` method in which I use: `SELECT * FROM name_table`. Why it could be that it retrieves to me just the first row? – Francisco Romero Jun 04 '15 at 17:06
  • Does the JSON look like this? "employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter","lastName":"Jones"} ] if u call **JSONArray jArray = jsonObject.getJSONArray("employees")** on this. it will work. – EE66 Jun 04 '15 at 17:08
  • No, just like this: {"firstName":"John", "lastName":"Doe"} but I think it is because it is retrieving me just the first row. – Francisco Romero Jun 04 '15 at 17:09
  • The issue is with the json. its not at the right format. You are sending parts of the json and thats ok. But the issue is 100% there. – EE66 Jun 04 '15 at 17:10
  • Do you mean with the GET method? – Francisco Romero Jun 04 '15 at 17:12
  • No, i think that the reqeust is ok. Becuase u get a response. i think that the response that u r receiving form the server isnt in the format that u expect. I cant solve this without seening most if not all of the response so will have to take my word for it and check it. – EE66 Jun 04 '15 at 17:15
  • But I think that the request it's making something wrong because if I put the `url` on my browser that makes reference to my `GET` method I have the same info that I get in the `responseString` so I think that this part it's correct. Isn't right? The problem now it's that I have 2 rows of values in my database and in my `url` I'm just getting 1 row but I don't know why. – Francisco Romero Jun 04 '15 at 17:19
  • Use Volley for the json request. You are using a very old framework for communication. There is no need for that now. Switch to Volley or any other updated fw and u will see exactly where the issue is. My guess from the information that u provided is that the server is sending a different format. – EE66 Jun 04 '15 at 17:21
  • Yes I now that it's an old framework but I need to use it (it's one of the requeriments). But I will considerate Volley for my next applications I will make. – Francisco Romero Jun 04 '15 at 18:02
  • Ok now I finally solve this problem with the JSON but now I still have the first one problem. How can I solve it? – Francisco Romero Jun 04 '15 at 18:26