【问题标题】:Get Response, Url and Url code in Textview在 Textview 中获取响应、Url 和 Url 代码
【发布时间】:2015-05-09 09:55:14
【问题描述】:

我正在尝试向 url 发出 post 请求,并获取显示在 TextView 中的 URL、响应代码和响应。我能够在日志中获取 URL 和响应代码,但无法获取响应。 请检查代码并告诉如何在TextView 中打印所有三个 谢谢

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {

String rescode,urlcode,responsecode;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new LongOperation().execute("");
}

private class LongOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        try {
            String url = "http://google.com";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj
                    .openConnection();



            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(
                    con.getOutputStream());

            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();

        ////////////////////////////////////////////////////////////////
            /*TextView tv2 = (TextView) findViewById(R.id.textView2);
            TextView tv3 = (TextView) findViewById(R.id.textView3);

            urlcode = url.getBytes().toString();
            rescode = responsecode.getBytes().toString();

            tv2.getText().toString();
            tv2.setText("URL-  "+urlcode);
            tv3.setText("ResponseCode-  "+rescode);*/

        /////////////////////////////////////////////////////////////////// 


            System.out.println("\nSending 'POST' request to URL : -----------------------" + url);
    //      System.out.println("Post parameters : " + urlParameters);
            System.out.println("Response Code : -------------------------------" + responseCode);


            BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result
            System.out.println("+++++++++"+response.toString());
        //  System.out.println("Response" +response);
            TextView tv = (TextView) findViewById(R.id.textView1);
            rescode = response.toString();

            tv.setText("aa"+rescode);
            return response.toString();

        } catch (Exception e) {
            System.out
                    .println("MainActivity.LongOperation.doInBackground()"+e);

        }
        return null;

    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}
}

日志

05-09 16:09:59.642: I/System.out(16309): Sending 'POST' request to URL : ---------------http://google.com

05-09 16:09:59.652: I/System.out(16309): Response Code : ------------------------------405

    050916:09:59.652:I/System.out(16309):MainActivity.LongOperation.doInBackground()java.io.FileNotFoundException: http://google.com

【问题讨论】:

    标签: android android-asynctask textview


    【解决方案1】:

    不要从 doInBackground 方法访问 UI

    试试下面的

     private class LongOperation extends AsyncTask<String, Void, String> {
    
    @Override
    protected String doInBackground(String... params) {
        try {
            String url = "http://google.com";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj
                    .openConnection();
    
    
    
            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(
                    con.getOutputStream());
    
            wr.flush();
            wr.close();
    
            int responseCode = con.getResponseCode();
    
        ////////////////////////////////////////////////////////////////
            /*TextView tv2 = (TextView) findViewById(R.id.textView2);
            TextView tv3 = (TextView) findViewById(R.id.textView3);
    
            urlcode = url.getBytes().toString();
            rescode = responsecode.getBytes().toString();
    
            tv2.getText().toString();
            tv2.setText("URL-  "+urlcode);
            tv3.setText("ResponseCode-  "+rescode);*/
    
        /////////////////////////////////////////////////////////////////// 
    
    
            System.out.println("\nSending 'POST' request to URL : -----------------------" + url);
    //      System.out.println("Post parameters : " + urlParameters);
            System.out.println("Response Code : -------------------------------" + responseCode);
    
    
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
           if(in!=null){
               while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
              }
            in.close();
           }
    
            // print result
            System.out.println("+++++++++"+response.toString());
        //  System.out.println("Response" +response);
    
            rescode = response.toString();
    
    
            return response.toString();
    
        } catch (Exception e) {
            System.out
                    .println("MainActivity.LongOperation.doInBackground()"+e);
    
        }
        return null;
    
    }
    
    @Override
    protected void onPostExecute(String result) {
        tv.setText("aa"+result);
    }
    
    @Override
    protected void onPreExecute() {
    }
    
    @Override
    protected void onProgressUpdate(Void... values) {
    }
    }
    }
    

    【讨论】:

    • 这一行是否打印了一些东西 System.out.println("+++++++++"+response.toString());
    • 那么 BufferedReader 有问题 in = new BufferedReader(new InputStreamReader( con.getInputStream()));字符串输入线; StringBuffer 响应 = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close();
    • 我已添加日志请检查
    • 你能告诉我要改变什么吗?
    • 我看到了你的日志猫,google.com 尝试更改链接,因为它在响应中没有返回任何内容
    猜你喜欢
    • 1970-01-01
    • 2011-09-22
    • 2016-07-02
    • 2019-06-05
    • 2015-04-26
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多