如何在Android API Level 22上通过POST Request发送JSON raw

时间:2015-08-04 19:13:26

标签: java android json

目前我正在调试我的Java代码,如下所示:

public void sign_in(View view) {
    String json = "";

    // The Username & Password
    final EditText em =  (EditText) findViewById(R.id.Username);
    String email = (String) em.getText().toString();
    final EditText pw =  (EditText) findViewById(R.id.Password);
    String password = (String) pw.getText().toString();
    // -----------------------


    JSONObject jsonObject = new JSONObject();
    try {
        HttpResponse response;
        jsonObject.accumulate("email", email);
        jsonObject.accumulate("password", password);
        json = jsonObject.toString();
        URL url = new URL("http://cloudspecinc.herokuapp.com/api/user/login/");
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url.toURI());
        httpPost.setEntity(new StringEntity(json, "UTF-8"));
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setHeader("Accept-Encoding", "application/json");
        httpPost.setHeader("Accept-Language", "en-US");
        response = httpClient.execute(httpPost);
        String sresponse = response.getEntity().toString();
    }
    catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());

    } finally {
        /* nothing to do here */
    }

}

我的问题:

  1. 如何获得POST请求的响应?它尚未进入 代码,因为我不知道如何获得它。
  2. 严重的问题:当我单击“登录”按钮时,应用程序始终 崩溃。 (注意:它在Android工作室编译但崩溃     当我点击“登录”按钮触发登录功能时。)
  3. 我认为问题是:

    1. 在Android API级别22中弃用了HttpClient。(Android 5.1 Lollipop API等级22)
    2. 代码有问题。

1 个答案:

答案 0 :(得分:7)

OK现在我知道..请求HTTP类型请求必须在AsyncTask上。不在当前线程上,因为由于网络线程异常而不在异步类上时会抛出异常。我忘记了Exception被称为什么。我只是Java的初学者。因为我不是真正的Java程序员。

以下是帮助其他人处理此类问题的代码。

public class REST extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        HttpURLConnection urlConnection=null;
        String json = null;
        // The Username & Password
        final EditText em =  (EditText) findViewById(R.id.Username);
        String email = (String) em.getText().toString();
        final EditText pw =  (EditText) findViewById(R.id.Password);
        String password = (String) pw.getText().toString();
        // -----------------------

        try {
            HttpResponse response;
            JSONObject jsonObject = new JSONObject();
            jsonObject.accumulate("email", email);
            jsonObject.accumulate("password", password);
            json = jsonObject.toString();
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://cloudspecinc.herokuapp.com/api/user/login/");
            httpPost.setEntity(new StringEntity(json, "UTF-8"));
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setHeader("Accept-Encoding", "application/json");
            httpPost.setHeader("Accept-Language", "en-US");
            response = httpClient.execute(httpPost);
            String sresponse = response.getEntity().toString();
            Log.w("QueingSystem", sresponse);
            Log.w("QueingSystem", EntityUtils.toString(response.getEntity()));
        }
        catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());

        } finally {
        /* nothing to do here */
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if (result != null) {
            // do something
        } else {
            // error occured
        }
    }
}