Android使用Json Post数据得到错误的结果

时间:2014-10-02 10:59:16

标签: android android-json android-parser

在这个简单的代码中,我将用户名和密码发送到服务器。如果那些是正确的我必须得到这个结果:

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

但我明白了:

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

对于此示例,我的用户名和密码是正确的。

我的代码问题是什么?我认为在使用HttpPost之后我必须得到另一种方法,例如HttpGet。是吗?

我的代码:

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();
    }
}

感谢。

1 个答案:

答案 0 :(得分:0)

我认为您在尝试连接时遇到问题..

试试这个方法

 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;

}