【问题标题】:HTTP Post Request giving NULL in Android在Android中给出NULL的HTTP Post请求
【发布时间】:2015-10-22 12:06:51
【问题描述】:

我正在尝试通过 API 发送一些数据。但是我的APP没有给出任何回应。既不给出任何错误也不停止。就像什么都没发生一样。我对android有点陌生,所以请指导。此外,我知道 HTTPClient 和其他导入已被弃用。

public void makePostRequest()
{
    class makePostRequestAsyncTask extends AsyncTask<Void, Void, String> {


        @Override
        protected String doInBackground(Void... params) {

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://103.226.216.209/finger_varification/api/finger_verification");
            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(7);
            nameValuePair.add(new BasicNameValuePair("sec_key", "2af8b9d956c39d2d52c46c2a02a978d1"));
            nameValuePair.add(new BasicNameValuePair("cnic", "3520214037921"));
            nameValuePair.add(new BasicNameValuePair("imei_no", "864121017028886"));
            nameValuePair.add(new BasicNameValuePair("device_name", "FingerMap5"));
            nameValuePair.add(new BasicNameValuePair("finger_index",index ));
            nameValuePair.add(new BasicNameValuePair("finger_template", "sdsd"));//model1.toString()));
            nameValuePair.add(new BasicNameValuePair("template_type", "RAW_IMAGE"));

            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
            } catch (UnsupportedEncodingException e) {
                // log exception
                e.printStackTrace();
            }


            //making POST request.
            try {
                HttpResponse response = httpClient.execute(httpPost);
                // write response to log

                Log.d("Http Post Response:", response.toString());
            } catch (ClientProtocolException e) {
                // Log exception
                e.printStackTrace();
            } catch (IOException e) {
                // Log exception
                e.printStackTrace();
            }

            return null;
        }
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            if(result.equals("working")){
                Toast.makeText(getApplicationContext(), "HTTP POST is working...", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(), "Invalid POST req...", Toast.LENGTH_LONG).show();
            }
        }


    }

}

【问题讨论】:

  • 嗨@Muhammad Abdullah,您的清单文件中有以下内容吗? &lt;uses-permission android:name="android.permission.INTERNET" /&gt;

标签: android apache android-activity http-post


【解决方案1】:

这是获取响应正文的方式

HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String response_body = reader.readLine();
Log.d("Http Post Response:", response_body);

顺便说一句,HttpPostHttpResponse 在 API 23 上已弃用,请改用 HttpURLConnection

【讨论】:

  • 完全弃用的意思?我想它会起作用的。你怎么看?此外......我不知道如何使用 HttpURLConnection 发送和接收......因为我有 7 个字符串要发送。
  • @MuhammadAbdullah:此处解释了弃用的含义stackoverflow.com/questions/8111774/deprecated-meaning 短篇小说,在 API 23 之后的版本中将不支持进一步的开发/错误修复。因此,您最好使用 HttpURLConnection,如NaviRamyle
  • @MuhammadAbdullah,关于 HttpURLConnection,我从我的个人小项目中获取了这段代码。 URL url = new URL(uri[0]); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); // The request will automatically be fired on demand when you want to get // any information about the HTTP response httpURLConnection.connect(); int statusCode = httpURLConnection.getResponseCode();
  • if (statusCode == 200) { BufferedReader inbf = new BufferedReader( new InputStreamReader(httpURLConnection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = inbf.readLine()) != null) { response.append(inputLine); } responseString = response.toString(); inbf.close(); }
猜你喜欢
  • 2016-01-25
  • 2012-07-10
  • 1970-01-01
  • 2020-02-15
  • 2021-09-26
  • 2015-06-26
  • 2013-02-03
  • 2017-11-28
  • 2023-03-22
相关资源
最近更新 更多