Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.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_Http Post - Fatal编程技术网

android中的HTTP POST请求

android中的HTTP POST请求,android,http-post,Android,Http Post,我需要向服务器发送http post请求,我想在android应用程序中发送字符串并作为JSON对象获得输出。如何使用async类完成此操作。请尝试以下代码:- class RequestAsyncTask extends AsyncTask<String, Integer, String> { @Override protected void onPreExecute() { { dialog = new Prog

我需要向服务器发送http post请求,我想在android应用程序中发送字符串并作为JSON对象获得输出。如何使用async类完成此操作。

请尝试以下代码:-

class RequestAsyncTask extends AsyncTask<String, Integer, String>
{

    @Override
    protected void onPreExecute()
    {

        {
            dialog = new ProgressDialog(Activity.this);
            dialog.setIndeterminate(true);
            dialog.setCancelable(false);
            dialog.show();
        }
    }

    @Override
    protected String doInBackground(String... params)
    {
        String status = "";
        try
        {
            HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 60000);
    HttpResponse response;

    try
    {

        HttpPost post = new HttpPost("url name");//

        System.out.println("jsonObject       " + jsonObject.toString());
        StringEntity stringEntity = new StringEntity(jsonObject.toString(), "UTF-8");
        post.setEntity(stringEntity);

        response = client.execute(post);

        if (response != null)
        {
            InputStream in = response.getEntity().getContent();
            status = convertStreamToString(in);// result comes  from server end
        }

    }
    catch (Exception e)
    {
        System.out.println("exception => " + e);
        e.printStackTrace();
    }
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return status;
    }

    @Override
    protected void onPostExecute(String result)
    {
        super.onPostExecute(result);
        try
        {
            System.out.println(result);
        }
        catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}


public String convertStreamToString(InputStream is) 
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try
        {
            while ((line = reader.readLine()) != null) 
            {
                sb.append(line + "\n");
            }
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        finally 
        {
            try 
            {
                is.close();
            }
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

请尝试下面的代码

new AsyncTask<String, Void, String>(){

        @Override
        protected String doInBackground(String... params) {
            HttpClient httpClient = new DefaultHttpClient();
            String url = "your_url";

            HttpPost httppost = new HttpPost(url);
            HttpResponse response;
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);

                nameValuePairs.add(new BasicNameValuePair("action", "action"));
                nameValuePairs.add(new BasicNameValuePair("action2", "action2"));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = httpClient.execute(httppost);
                HttpEntity entity = response.getEntity();

                if(entity != null){
                    String str = EntityUtils.toString(entity);
                    //do your json here                     
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } 
            return null; 
        }

    }.execute(null,null,null);

有这么多与AsyncTask相关的示例,请仔细阅读此

,然后如何调用此类来发送请求并获取响应New RequestAsyncTask.execute;它会执行,但不会返回ResponseOnPostExecute打印结果I,什么是CreateReturnRequest?我如何调用这个类来发送请求和获取响应
new AsyncTask<String, Void, String>(){

        @Override
        protected String doInBackground(String... params) {
            HttpClient httpClient = new DefaultHttpClient();
            String url = "your_url";

            HttpPost httppost = new HttpPost(url);
            HttpResponse response;
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);

                nameValuePairs.add(new BasicNameValuePair("action", "action"));
                nameValuePairs.add(new BasicNameValuePair("action2", "action2"));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = httpClient.execute(httppost);
                HttpEntity entity = response.getEntity();

                if(entity != null){
                    String str = EntityUtils.toString(entity);
                    //do your json here                     
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } 
            return null; 
        }

    }.execute(null,null,null);