Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中HTTP Post请求提供空值_Android_Apache_Android Activity_Http Post - Fatal编程技术网

Android中HTTP Post请求提供空值

Android中HTTP Post请求提供空值,android,apache,android-activity,http-post,Android,Apache,Android Activity,Http Post,我正在尝试通过API发送一些数据。然而,我的应用程序没有给出任何响应。既不出错也不被阻止。好像什么都没发生。我对安卓系统有点陌生,所以请给我一个亲切的指导。此外,我知道HTTPClient和其他导入已被弃用 public void makePostRequest() { class makePostRequestAsyncTask extends AsyncTask<Void, Void, String> { @Override prote

我正在尝试通过API发送一些数据。然而,我的应用程序没有给出任何响应。既不出错也不被阻止。好像什么都没发生。我对安卓系统有点陌生,所以请给我一个亲切的指导。此外,我知道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();
            }
        }


    }

}
public void makePostRequest()
{
类makePostRequestAsyncTask扩展了AsyncTask{
@凌驾
受保护字符串doInBackground(无效…参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://103.226.216.209/finger_varification/api/finger_verification");
List nameValuePair=新的ArrayList(7);
nameValuePair.add(新的基本nameValuePair(“sec_密钥”,“2af8b9d956c39d2d52c46c2a02a978d1”);
添加(新的BasicNameValuePair(“cnic”,“3520214037921”);
添加(新的基本名称对(“imei_编号”,“864121017028886”);
添加(新的BasicNameValuePair(“设备名称”、“FingerMap5”);
添加(新的BasicNameValuePair(“finger_index”,index));
添加(新的BasicNameValuePair(“finger_模板”、“sdsd”);//model1.toString());
添加(新的BasicNameValuePair(“模板类型”、“原始图像”);
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePair));
}捕获(不支持的编码异常e){
//日志异常
e、 printStackTrace();
}
//提出邮寄请求。
试一试{
HttpResponse response=httpClient.execute(httpPost);
//将响应写入日志
Log.d(“Http Post响应:,Response.toString());
}捕获(客户端协议例外e){
//日志异常
e、 printStackTrace();
}捕获(IOE异常){
//日志异常
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
如果(结果等于(“工作”)){
Toast.makeText(getApplicationContext(),“HTTP POST正在工作…”,Toast.LENGTH\u LONG.show();
}否则{
Toast.makeText(getApplicationContext(),“无效的POST请求…”,Toast.LENGTH_LONG.show();
}
}
}
}

这是获得响应主体的方式

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中被弃用,请使用代替Hi@Muhammad Abdullah,您的清单文件中有以下内容吗<代码>不推荐的确切含义是什么?我想它会起作用的。你怎么认为?此外……我不知道如何使用HttpURLConnection发送和接收……因为我有7个字符串要发送。@MuhammadAbdullah:不推荐的含义在这里解释了简短的故事,在API 23之后的版本中,不支持进一步的开发/错误修复。因此,您最好使用NaviRamyle@MuhammadAbdullah,关于HttpURLConnection,我从我个人的小项目中获取了这段代码<代码>URL=新URL(uri[0]);HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();httpURLConnection.setRequestMethod(“GET”);//当您想要获取//有关HTTP响应httpURLConnection.connect()的任何信息时,请求将根据需要自动触发;int statusCode=httpURLConnection.getResponseCode()
如果(statusCode==200){BufferedReader inbf=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream());字符串inputLine;StringBuffer响应=new StringBuffer();而((inputLine=inbf.readLine())!=null){response.append(inputLine);}responseString=response.toString();inbf.close();}