我一直在Android中的HttpUrlConnection和BackGroundTask中遇到这些错误

时间:2017-02-02 02:11:27

标签: php android json parsing

以下是logcat中的错误结果:

Image 2

这是我的代码:

Image 1

1 个答案:

答案 0 :(得分:0)

你有没有添加

<uses-permission android:name="android.permission.INTERNET" />

进入AndroidManifest.xml? 您可以尝试使用方法发布到服务器的代码。

@覆盖

protected Void doInBackground(Void... params) {


    try {


        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("https://192.168.2.95/LAfinal/api/login_student.php");

        // ADD YOUR DATA
        List<NameValuePair> nameValuePairs = new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("id", _activity.LOGINED_USER_ID));
        Log.v("HTTP", "Response__activity.LOGINED_USER_ID: " + String.valueOf(_activity.LOGINED_USER_ID));
        nameValuePairs.add(new BasicNameValuePair("token", _activity.fcmRegId));
        Log.v("HTTP", "Response___activity.fcmRegId: " + _activity.fcmRegId);
        nameValuePairs.add(new BasicNameValuePair("status", String.valueOf(_notificationStatus)));
        Log.v("HTTP", "Response_notificationStatus: " + String.valueOf(_notificationStatus));
        nameValuePairs.add(new BasicNameValuePair("type", "android"));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // EXECUTE HTTP POST REQUEST
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity resEntity = response.getEntity();
        //resEntity.getContent().close();
        int status = response.getStatusLine().getStatusCode();
        if (status == 200) {
            if (_notificationStatus == 1) {
                WebViewActivity._needLogin = false;
            } else {
                WebViewActivity._needLogin = true;
            }
        }

        if (resEntity != null) {
            String responseStr = EntityUtils.toString(resEntity).trim();
            Log.v("HTTP", "Response: " + responseStr);
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}`