我一直在玩Android Studio,并尝试与开发环境建立连接,在开发环境中以JSON响应。我已经测试了我的代码和Eclipse(不是ADK),并且工作正常。我已经将权限添加到的AndroidManifest.xml
文件中INTERNET
,ACCESS_NETWORK_STATE
因此已经涵盖了。我不知道该怎么办?
我的Java:
public class GetClientDetails {
public void jsonResponse () {
try{
URL url = new URL("http://invsydmdev069:7080/IFXJSON?type=PartyRq&RqUID=123456789&PartyId=1093300");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept", "application/json");
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader (is, "UTF-8") );
try {
String preJsonObj = reader.readLine();
JSONObject jsonObj = new JSONObject(preJsonObj);
String strPartyId = jsonObj.getJSONObject("PartyRs").getJSONObject("PartyRec").getString("PartyId");
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
catch(Exception e)
{
System.out.println("its fked");
}
}
}
这android.os.NetworkOnMainThreadException
是自我解释。您不应在主线程上执行任何导致UI无响应的网络操作。您需要在中运行网络操作AsyncTask
。
在SO上查看此帖子。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句