java - 如何在 Android API 级别 22 上通过 POST 请求发送 JSON raw

标签 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 studio 上编译但崩溃 当我点击触发登录功能的“登录”按钮时)。

我认为问题是:

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

最佳答案

好的,现在我知道了。请求 HTTP 类型请求必须在 AsyncTask 上。不在当前线程上,因为由于网络线程异常,当它不在异步类上时会抛出异常。我忘记了异常叫什么。我只是 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
        }
    }
}

关于java - 如何在 Android API 级别 22 上通过 POST 请求发送 JSON raw,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31817404/

相关文章:

java - 从外部服务器在 Thymeleaf 中渲染 html 片段

java - 互动影像

Android ViewPagerIndicator 设置标题

android - 如何从 Google Fit 历史记录中获取心率值?

php - 将列名作为 json 对象属性公开是否存在安全风险?

java - Oracle SqlDeveloper 找不到正确的 JDK

android - 这是实现 AsyncTask 的最佳方式吗?或者还有更好的方法吗?

javascript - ExtJS4:TreePanel Store 正在通过 Itemclick 一次又一次地加载 json 文件

objective-c - iOS UITextView 忽略 JSON 响应中的换行符

java - Service Fabric Java 应用程序部署在本地集群中失败