简体   繁体   中英

my android application is not geting json data from the server

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StrictMode.enableDefaults();
    resultview=(TextView)findViewById(R.id.resulttext);
    getdata();

}

public void getdata()
{
    String result=null;
    InputStream inputstream=null;
    try{
        HttpClient httpclient=new DefaultHttpClient();
        HttpPost httppost=new HttpPost("http://127.0.0.1/getallcustomers.php");
        HttpResponse response=httpclient.execute(httppost);
        HttpEntity httpentity=response.getEntity();
        inputstream= httpentity.getContent();
    }
    catch(Exception ex)
    {
        resultview.setText(ex.getMessage()+"at 1st exception");
    }
    try{
        BufferedReader reader=new BufferedReader(new InputStreamReader(inputstream,"iso-8859-1"),8);
        StringBuilder sb=new StringBuilder();
        String line=null;
        while((line=reader.readLine())!= null)                  
        {
            sb.append(line +"\n");
        }
        inputstream.close();
        result=sb.toString();
    }
    catch(Exception ex)
    {
        resultview.setText(ex.getMessage()+"at 2nd exception");
    }
    try{
        String s="test :";
        JSONArray jarray=new JSONArray(result);
        for(int i=0; i<jarray.length();i++)
        {
            JSONObject json=jarray.getJSONObject(i);
            s= s +
                    "Name : "+json.getString("firstname")+" "+json.getString("lastname")+"\n"+
                    "age :"+json.getString("age")+"\n"+
                    "phone : "+json.getString("phone");
        }
        resultview.setText(s);
    }
    catch(Exception ex)
    {
        this.resultview.setText(ex.getMessage()+"at 3rd exception");
    }

}

}

Are you sure you can access your localhost from the emulator? You should take a look at this: Accessing localhost:port from Android emulator

You are launching the http request on UI thread. Probably it crashes for this.

Try to log the exception, not put in your resultview object and confirm that

is your php version 5.5?

you should use mysqli not use mysql

the php code is correct, perhaps the access database does not have the correct data

Code to get json data from server. It is working fine for me.

private void get_valueFromServer(String php) {
String responseString = null;
try{    
    HttpClient httpclient = new DefaultHttpClient();
    String url ="your_url"; 
    HttpPost httppost = new HttpPost(url);
    HttpResponse response = httpclient.execute(httppost);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    response.getEntity().writeTo(out);
    out.close();
    responseString = out.toString();
    Toast.makeText(getApplicationContext(),responseString,1000).show();

    //The below code is for Separating values.It may varies according with your result from server.

    JSONArray ja = new JSONArray(responseString);
    int x=Integer.parseInt(ja.getJSONObject(0).getString("your_string_name"));

} catch(Exception e) {
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM