hi hello I am new to android.I am getting response from server. But i get the following response..
getting the following error
Value {"Elec":"Cup1"} at 1 of type java.lang.String cannot be converted to JSONObject
The responce is as follows
{"Electronics":["{\"Elec\":\"Cup0\"}","{\"Elec\":\"Cup1\"}","{\"Elec\":\"Cup2\"}","{\"Elec\":\"Cup3\"}","{\"Elec\":\"Cup4\"}","{\"Elec\":\"Cup5\"}","{\"Elec\":\"Cup6\"}","{\"Elec\":\"Cup7\"}","{\"Elec\":\"Cup8\"}","{\"Elec\":\"Cup9\"}"],"Printable":["{\"Print\":\"Mug0\"}","{\"Print\":\"Mug1\"}","{\"Print\":\"Mug2\"}","{\"Print\":\"Mug3\"}","{\"Print\":\"Mug4\"}","{\"Print\":\"Mug5\"}","{\"Print\":\"Mug6\"}","{\"Print\":\"Mug7\"}","{\"Print\":\"Mug8\"}","{\"Print\":\"Mug9\"}"]}
And i send and receive the responce is as follows
class SliderImages extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... urls) {
return POST1(urls[0]);
}
@Override
public void onPostExecute(String result) {
}
}
public String POST1(String url) {
InputStream inputStream;
String result = "";
try {
arraylist = new ArrayList<HashMap<String, String>>();
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
Log.d("JsonStringSend", json);
// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if (inputStream != null) {
result = convertInputStreamToString(inputStream);
Log.d("JSONResponce", result);
JSONObject jsonObj = new JSONObject(result);
contacts = jsonObj.getJSONArray("Electronics");
for (int i = 1; i < contacts.length()-1; i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("Print");
Log.e("Errrrrrrr",""+id);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
// 11. return result
return result;
}
private String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
So the get the following error
Value {"Elec":"Cup1"} at 1 of type java.lang.String cannot be converted to JSONObject
Please help me
Thanks in advance
