亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

android接收不到服務器端發送回來的數據,請問下是為什么

android接收不到服務器端發送回來的數據,請問下是為什么

qq__3574 2017-03-21 01:21:57
public class MainActivity extends Activity { ? ?private Button send; ? ?private EditText input; ? ?private TextView output; ? ?@Override ? ?protected void onCreate(Bundle savedInstanceState) { ? ? ? ?super.onCreate(savedInstanceState); ? ? ? ?setContentView(R.layout.activity_main); ? ? ? ?input=(EditText)this.findViewById(R.id.input); ? ? ? ?output=(TextView)this.findViewById(R.id.output); ? ? ? ?send=(Button) this.findViewById(R.id.send); ? ? ? ?send.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ?@Override ? ? ? ? ? ?public void onClick(View view) { ? ? ? ? ? ? ? ?final String Output=input.getText().toString().trim(); ? ? ? ? ? ? ? ?new Thread(new Runnable() { ? ? ? ? ? ? ? ? ? ?@Override ? ? ? ? ? ? ? ? ? ?public void run() { ? ? ? ? ? ? ? ? ? ? ? ?String path="http://192.168.11.254:8080"; ? ? ? ? ? ? ? ? ? ? ? ?//String path="http://192.168.111.93"; ? ? ? ? ? ? ? ? ? ? ? ?try { ? ? ? ? ? ? ? ? ? ? ? ? ? ?URL url=new URL(path); ? ? ? ? ? ? ? ? ? ? ? ? ? ?//打開http連接 ? ? ? ? ? ? ? ? ? ? ? ? ? ?HttpURLConnection conn=(HttpURLConnection) url.openConnection(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?conn.setDoInput(true); ? ? ? ? ? ? ? ? ? ? ? ? ? ?conn.setDoOutput(false); ? ? ? ? ? ? ? ? ? ? ? ? ? ?conn.setRequestMethod("POST"); ? ? ? ? ? ? ? ? ? ? ? ? ?// ?conn.setRequestMethod("GET"); ? ? ? ? ? ? ? ? ? ? ? ? ? ?conn.setConnectTimeout(1000*30);//連接超時時間 ? ? ? ? ? ? ? ? ? ? ? ? ? ?conn.setReadTimeout(1000*30);//讀取數據的超時時間 ? ? ? ? ? ? ? ? ? ? ? ? ? ?conn.setUseCaches(false);// ? ? ? ? ? ? ? ? ? ? ? ? ? ?conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// ? ? ? ? ? ? ? ? ? ? ? ? ? ?conn.setRequestProperty("Connection", "Keep-Alive");// 維持長連接// ? ? ? ? ? ? ? ? ? ? ? ? ? ?conn.setRequestProperty("Charset", "UTF-8");// ? ? ? ? ? ? ? ? ? ? ? ? ? ?conn.setRequestProperty("Content-type", "application/x-java-serialized-object"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); ? ? ? ? ? ? ? ? ? ? ? ? ? ?conn.connect(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?//對服務器端讀取或寫入數據 ? ? ? ? ? ? ? ? ? ? ? ? ? ?//DataOutputStream out=new DataOutputStream(conn.getOutputStream()); ? ? ? ? ? ? ? ? ? ? ? ? ? ?OutputStream out=conn.getOutputStream(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?out.write(Output.getBytes()); ? ? ? ? ? ? ? ? ? ? ? ? ? ?//out.writeBytes(Output); ? ? ? ? ? ? ? ? ? ? ? ? ? ?out.flush(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?out.close(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?//獲取響應數據 ? ? ? ? ? ? ? ? ? ? ? ? ? ?int code=conn.getResponseCode(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?String sCurrentLine=""; ? ? ? ? ? ? ? ? ? ? ? ? ? ?String sTotalString=""; ? ? ? ? ? ? ? ? ? ? ? ? ? ?if(code==200){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?InputStream os=conn.getInputStream(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?BufferedReader reader=new BufferedReader(new InputStreamReader(os)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?while((sCurrentLine=reader.readLine())!=null){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if(sCurrentLine.length()>0){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sTotalString=sTotalString+sCurrentLine.trim(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?final String finalLine = sTotalString; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?runOnUiThread(new Runnable() { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?@Override ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?public void run() { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?output.setText("內容:"+ finalLine); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Toast.makeText(MainActivity.this,"try!!!",Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}); ? ? ? ? ? ? ? ? ? ? ? ? ? ?}else{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Toast.makeText(MainActivity.this,"fail!",Toast.LENGTH_LONG).show(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ? ? ? ? ? ?//InputStreamReader in=new InputStreamReader(conn.getInputStream());// ? ? ? ? ? ? ? ? ? ? ? ? ? ? BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));// ? ? ? ? ? ? ? ? ? ? ? ? ? ?//BufferedReader br=new BufferedReader(in);// ? ? ? ? ? ? ? ? ? ? ? ? ? ?String readLine;// ? ? ? ? ? ? ? ? ? ? ? ? ? ?StringBuffer strbuffer=new StringBuffer();// ? ? ? ? ? ? ? ? ? ? ? ? ? ?String line=null;// ? ? ? ? ? ? ? ? ? ? ? ? ? ?while((readLine=br.readLine())!=null){//// ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?line+=readLine;// ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?strbuffer.append(readLine);// ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?strbuffer.append("\n");// ? ? ? ? ? ? ? ? ? ? ? ? ? ?}//// ? ? ? ? ? ? ? ? ? ? ? ? ? ?line=strbuffer.toString();// ? ? ? ? ? ? ? ? ? ? ? ? ? ?br.close(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?//System.out.println("result"+result); ? ? ? ? ? ? ? ? ? ? ? ? ? ?conn.disconnect();//關閉連接 ? ? ? ? ? ? ? ? ? ? ? ?} catch (MalformedURLException e) { ? ? ? ? ? ? ? ? ? ? ? ? ? ?e.printStackTrace(); ? ? ? ? ? ? ? ? ? ? ? ?} catch (IOException e) { ? ? ? ? ? ? ? ? ? ? ? ? ? ?e.printStackTrace(); ? ? ? ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ?}).start(); ? ? ? ? ? ?} ? ? ? ?}); ? ?}}
查看完整描述

2 回答

?
康6

TA貢獻3條經驗 獲得超5個贊

首先測試下接口是否有問題, ? 然后看看log ? , 或者用 Xutil okhttp ?等框架試試


查看完整回答
反對 回復 2017-03-21
  • qq__3574
    qq__3574
    我測試了下,發現連接超時了,這要怎么解決,我也不清楚哪里錯了
?
田心楓

TA貢獻1064條經驗 獲得超383個贊

看返回的結果不就行了?或者抓下包
查看完整回答
反對 回復 2017-03-21
  • qq__3574
    qq__3574
    要如何查看就是返回結果時看不到
  • wolf_0005
    wolf_0005
    1. 要學會看日志 2. 有一個問題你要先知道,如果是用安卓模擬器或手機你要先檢測http://192.168.111.93是否能夠打開(不是在電腦上查看),沒有形成局域網也是一種原因。
  • 2 回答
  • 0 關注
  • 2567 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號