我正在尝试向网址发布帖子请求,并在TextView
中显示网址,响应代码和响应。我能够在日志中获取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
答案 0 :(得分:4)
不要从doInBackground
方法
尝试以下
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) {
}
}
}