androidandroid-jsonandroid-parser

Android get Wrong result using Json Post data


in this simple code i'm sending username and password to server. if those are correct i must be get this result :

{"code":"1","credit":100000,"number":["500000072207"]}

but i get:

10-02 14:16:52.280    2299-2318/com.ms.app.ms E/Content﹕ org.apache.http.conn.EofSensorInputStream@4164ca78

for this sample my username and password is correct.

whats my problem in code? i think after using HttpPost i must be get result another method such as HttpGet. is that right?

My Code:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sendJson("name","pass");
    }

    protected void sendJson(final String username, final String password) {
        Thread t = new Thread() {

            public void run() {
                Looper.prepare(); //For Preparing Message Pool for the child Thread
                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                HttpResponse response;
                JSONObject json = new JSONObject();

                try {
                    HttpPost post = new HttpPost("http://www.example.com/test.php");
                    json.put("username", username);
                    json.put("password", password);
                    StringEntity se = new StringEntity( json.toString());
                    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);
                    response = client.execute(post);

                    if(response!=null){
                        //Get the data in the entity
                        InputStream in = response.getEntity().getContent();
                        Log.e("Content", String.valueOf(in));
                    }

                } catch(Exception e) {
                    e.printStackTrace();
                    Log.e("Error", String.valueOf(e));
                }

                Looper.loop(); //Loop in the message queue
            }
        };

        t.start();
    }
}

Thanks.


Solution

  • I think you are having a problem while you try to get connection..

    Try this method

     public JSONObject makeHttpRequest(String url, String method,List<NameValuePair> params) {
    
        // Making HTTP request
        try {
    
            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));
    
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
    
            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);
    
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }           
    
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
    
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
    
        // return JSON String
        return jObj;
    
    }