0

I am new to android. Well what I am trying to do in the following code is making an http request sending to a php file from where json gets the response. In the php file I am trying to show the city names starting from A. Whenever I am running the code I am getting this error: "org.json.jsonexception value doctype of type java.lang.string cannot be converted to jsonarray". All The related Threads are of no help or i am not able to understand the solution given there. Here is my Java Code

MainActivity.java

package com.list;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;

import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;


int ct_id;
String ct_name;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


TextView textview1 = (TextView)findViewById(R.id.textView1);
textview1.setText("Erqem");

 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
     HttpClient httpclient = new DefaultHttpClient();
     HttpPost httppost = new HttpPost("http://10.0.2.2/city.php");
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
     HttpResponse response = httpclient.execute(httppost);
     HttpEntity entity = response.getEntity();
     is = entity.getContent();

     }catch(Exception e){
         Log.e("log_tag", "Error in http connection"+e.toString());
    }
//convert response to string
try{
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
       sb = new StringBuilder();
       sb.append(reader.readLine() + "\n");

       String line="0";
       while ((line = reader.readLine()) != null) {
                      sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
        }catch(Exception e){
              Log.e("log_tag", "Error converting result "+e.toString());
        }

//paring data
try{
      jArray = new JSONArray(result);
      JSONObject json_data = new JSONObject();
      json_data=null;
      for(int i=0;i<jArray.length();i++){
             json_data = jArray.getJSONObject(i);
             ct_id=json_data.getInt("CITY_ID");
             ct_name=json_data.getString("CITY_NAME");

             textview1.setText(ct_name);
             }

      }

      catch(JSONException e1){
          Toast.makeText(getBaseContext(), "No City Found"+e1.toString() ,Toast.LENGTH_LONG).show();
          Log.e("log_tag", "Error converting result "+e1.getMessage() );
      } catch (ParseException e1) {
            e1.printStackTrace();
    }
}
}

php file

<?php
header('Content-type=application/json; charset=utf-8');
mysql_connect("","root","");
 mysql_select_db("Deal");
 $sql=mysql_query("select * from CITY where CITY_NAME like 'A%'");
 while($row=mysql_fetch_assoc($sql)) 
 $output[]=$row; 
 print(json_encode($output));
 mysql_close();

?>

Any Help would be appreciated.Thanks.

Community
  • 1
  • 1
james
  • 3,934
  • 11
  • 41
  • 60
  • 1
    You should log the response you get before you try to parse it and check what you get. Have you tried to run the url from the emulator browser? – WarrenFaith Apr 01 '13 at 21:16
  • what is the output you are trying to parse? please update your question with it – laplasz Apr 01 '13 at 21:19
  • @WarrenFaith As i am very much new in android. could you please explain your point in more detail? – james Apr 01 '13 at 21:19
  • 1
    If I were a betting man (and I'm not), I'd say you're getting an error message back from the server, rather than json. Log your response, like @WarrenFaith said. – 323go Apr 01 '13 at 21:19
  • Furthermore, your code will CRASH any modern (4.x+) Android device with a NetworkOnMainThreadException. – 323go Apr 01 '13 at 21:20
  • Log.d("string to convert:",result); – laplasz Apr 01 '13 at 21:21
  • @323go How should I log my response? It would be great if you gave me an answer – james Apr 01 '13 at 21:25
  • what is the result of this log - you can find it in LogCat, are you using Eclipse? http://stackoverflow.com/questions/3280051/how-to-enable-logcat-console-in-eclipse-for-android – laplasz Apr 01 '13 at 21:25
  • @laplasz the same as written above org.json.jsonexception value doctype of type java.lang.string cannot be converted to jsonarray – james Apr 01 '13 at 21:28
  • @laplasz Yes I am using eclipse and my logcat is activated – james Apr 01 '13 at 21:28
  • please put this log statement before jArray = new JSONArray(result); – laplasz Apr 01 '13 at 21:30
  • @laplasz I posted this Log.d("log_tag", " converting " ); before the jArray = new JSONArray(result); and could see "converting" in logcat – james Apr 01 '13 at 21:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27355/discussion-between-laplasz-and-james) – laplasz Apr 01 '13 at 21:35

1 Answers1

2

maybe you are using incorrect address, please try 10.0.2.2:8080/city.php

laplasz
  • 3,111
  • 1
  • 25
  • 35