问题描述
- 网络请求有问题,一直调试不了
-
10-17 22:07:04.490: D/libc(21273): [NET] android_getaddrinfofornet+,hn 23(0x7363686f6f6c75),sn(),hints(known),family 0,flags 4
10-17 22:07:04.490: D/libc(21273): [NET] android_getaddrinfofornet-, err=8
10-17 22:07:04.490: D/libc(21273): [NET] android_getaddrinfofornet+,hn 23(0x7363686f6f6c75),sn(),hints(known),family 0,flags 1024
10-17 22:07:04.490: D/libc(21273): [NET] android_getaddrinfofornet-, pass to proxy
10-17 22:07:04.490: D/libc(21273): [NET] android_getaddrinfo_proxy+
10-17 22:07:04.490: D/libc(21273): [NET] android_getaddrinfo_proxy get netid:0
10-17 22:07:04.510: I/System.out(21273): null
10-17 22:07:04.550: D/libc(21273): [NET] android_getaddrinfo_proxy-, success
10-17 22:07:04.670: W/System.err(21273): java.net.ProtocolException: cannot write request body after response has been read
10-17 22:07:04.670: W/System.err(21273): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:214)
10-17 22:07:04.670: W/System.err(21273): at com.example.mydemo.Aty$2.run(Aty.java:80)
10-17 22:07:04.670: W/System.err(21273): at java.lang.Thread.run(Thread.java:818)代码如下
public class Aty extends Activity {private TextView textview;
String result;
String newss;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
if(msg.what==1){System.out.println(result); textview.setText(newss); } } };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textview=(TextView)findViewById(R.id.text);
new Thread(new Runnable(){
Intent intent=getIntent();
int id=intent.getIntExtra("id",1);String[] date = {"http://schoolunify.sinaapp.com/index.php/news_c/reNewsContent_IOS", "news_id="+id}; @Override public void run() { // TODO Auto-generated method stub URL HttpUrl; try { HttpUrl = new URL(date[0]); HttpURLConnection con= (HttpURLConnection)HttpUrl.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setRequestMethod("POST"); int responseCode = con.getResponseCode(); if (responseCode==200) { OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(),"UTF-8"); BufferedWriter bw = new BufferedWriter(osw); bw.write(date[1]); bw.flush(); StringBuffer sb = new StringBuffer(); String s; BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); while((s=br.readLine())!=null){ sb.append(s); } result= sb.toString(); osw.close(); bw.close(); br.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); Message msg= new Message(); msg.what = 1; handler.sendMessage(msg);
}
private void readJson(String s ) {
try { JSONObject obj = new JSONObject();
obj.put("content", s);
System.out.println(obj);
newss =obj.getString("content");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
解决方案
http://blog.csdn.net/zuolongsnail/article/details/6373051