android HttP请求服务器,连接超时

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imagebtn:
new Thread() {
URL url;
HttpURLConnection conn = null;
OutputStream os = null;
BufferedReader in = null;

@Override
public void run() {
Looper.prepare();
try {
PreferencesService preferences = new PreferencesService(
getActivity());
String tempURL = preferences
.getPreferences(AllFinalInfo.SMS_IP_PORT_KEY);
// if(tempURL==null || "".equals(tempURL.trim())){
// Toast.makeText(getActivity(), "请设置上传的IP地址和端口!",
// Toast.LENGTH_SHORT).show();
// return;
// }
Toast.makeText(getActivity(), "tempURL:" + tempURL,
Toast.LENGTH_SHORT).show();
url = new URL(AllFinalInfo.SERVER_URL);

for (int i = 0; i < listMsg.size(); i++) {
/* 封装子对象 */
JSONObject ClientKey = new JSONObject();
ClientKey.put("sms__id", listMsg.get(i)
.getSms__id());
ClientKey.put("sms_thread_id", listMsg.get(i)
.getSms_thread_id());
ClientKey.put("sms_body", listMsg.get(i)
.getSms_body());
ClientKey.put("sms_address", listMsg.get(i)
.getSms_address());
ClientKey.put("sms_person", listMsg.get(i)
.getSms_person());
ClientKey.put("sms_body", listMsg.get(i)
.getSms_body());
ClientKey.put("sms_status", listMsg.get(i)
.getSms_status());
ClientKey.put("sms_date", listMsg.get(i)
.getSms_date());
ClientKey.put("sms_type", listMsg.get(i)
.getSms_type());
ClientKey.put("sms_protocol", listMsg.get(i)
.getSms_protocol());
ClientKey.put("sms_read", listMsg.get(i)
.getSms_read());
ClientKey.put("sms_service_center", listMsg.get(i)
.getSms_service_center());

/* 把JSON数据转换成String类型使用输出流向服务器写 */
String content = String.valueOf(ClientKey);

conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoOutput(true);
// 设置允许输出
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "Fiddler");
conn.setRequestProperty("Content-Type",
"application/json");
os = conn.getOutputStream();
os.write(content.getBytes());
os.close();
}
/* 服务器返回的响应码 */
int code = conn.getResponseCode();
if (code == 200) {
// in = new BufferedReader(new InputStreamReader(
// conn.getInputStream(), "utf-8"));
// String retData = null;
// String responseData = "";
// while ((retData = in.readLine()) != null) {
// responseData += retData;
// }
// JSONObject jsonObject = new
// JSONObject(responseData);
// JSONObject succObject =
// jsonObject.getJSONObject("smsBean");
//
// //errormsg:表示服务器执行是否成功的消息 type:String
// String backMsg =
// succObject.getString("errormsg");
// //errorcode:0 表示成功 -100表示失败 type:Int
// String backStatus=
// succObject.getString("errorcode");
// //code 数据库执行是状态 0表示成功 -100表示是失败 type:int
// String dbstatus = succObject.getString("code");
// //msg:数据库对于短信格式的验证:是否成功 type:String
// String dbMsg = succObject.getString("msg");
//
// //定义个字符串拼接消息
// StringBuffer buffer=new StringBuffer();
// if(backStatus.equals("0")&&backStatus!=null){
// buffer.append("服务器接收数据成功、");
// }else if(dbstatus.equals("0")&&dbstatus!=null){
// buffer.append("数据库执行成功");
// }
// Toast.makeText(getActivity(),
// buffer,Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), "数据提成功",
Toast.LENGTH_SHORT).show();
Looper.loop();
} else {
Toast.makeText(getActivity(), "数据提交失败",
Toast.LENGTH_SHORT).show();
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {

try {
if (in != null) {
in.close();
}
} catch (IOException o) {
o.printStackTrace();
}
}
}
}.start();
break;

default:
break;
}
}






大神,求助,不知道怎么回事儿就是连接不成功








你可能感兴趣的:(android HttP请求服务器,连接超时)