所以我为 Android 编写了一个 Java 类来从网站获取 JSON 并解析它:
public class Communicator {
public String executeHttpGet(String URL) throws Exception
{
BufferedReader in = null;
try
{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(URL));
HttpResponse response = client.execute(request);
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
StringBuilder sb = new StringBuilder();
String s;
while(true )
{
s = buf.readLine();
if(s==null || s.length()==0)
break;
sb.append(s);
}
buf.close();
ips.close();
return sb.toString();
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
Log.d("Exceptions !", e.toString());
}
}
}
}
}
所以这是我尝试访问的站点,这是示例输出:
http://www.railpnrapi.com/6520652825
{"tnum":"15694","tname":"BARAK VALLEY EX","tdate":"25-7-2013","from":"SCL","to":"LMG","class" :"SL","charted":true,"numofpax":1,"pax":[{"coach":"S1","berth":"8","quota":"GN","status" :"CNF"}]}
这是读取 JSON 的不完整代码:
String page = new Communicator().executeHttpGet(url);
System.out.println(page)
JSONObject jsonObj = new JSONObject(page);
String id = jsonObj.getString("tname");
TextView t1 = (TextView)findViewById(R.id.textView1);
t1.setText(id);
我试图将 JSON 的一小部分输出到 TextView,但这不起作用。这是否意味着我必须事先完全解析它?
LogCat 也显示此错误:
07-02 06:18:08.889: W/System.err(831): android.os.NetworkOnMainThreadException
07-02 06:18:08.899: W/System.err(831): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-02 06:18:08.899: W/System.err(831): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-02 06:18:08.899: W/System.err(831): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-02 06:18:08.899: W/System.err(831): at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-02 06:18:08.899: W/System.err(831): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-02 06:18:08.899: W/System.err(831): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-02 06:18:08.899: W/System.err(831): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-02 06:18:08.899: W/System.err(831): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-02 06:18:08.909: W/System.err(831): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-02 06:18:08.909: W/System.err(831): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-02 06:18:08.909: W/System.err(831): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-02 06:18:08.909: W/System.err(831): at com.example.in.ddas.indianrailpnr.Communicator.executeHttpGet(Communicator.java:27)
07-02 06:18:08.909: W/System.err(831): at com.example.in.ddas.indianrailpnr.PNRActivity$1.onTouch(PNRActivity.java:74)
07-02 06:18:08.919: W/System.err(831): at android.view.View.dispatchTouchEvent(View.java:7241)
07-02 06:18:08.919: W/System.err(831): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-02 06:18:08.919: W/System.err(831): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-02 06:18:08.919: W/System.err(831): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-02 06:18:08.919: W/System.err(831): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-02 06:18:08.919: W/System.err(831): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-02 06:18:08.929: W/System.err(831): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-02 06:18:08.929: W/System.err(831): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-02 06:18:08.929: W/System.err(831): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-02 06:18:08.929: W/System.err(831): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-02 06:18:08.929: W/System.err(831): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-02 06:18:08.939: W/System.err(831): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
07-02 06:18:08.939: W/System.err(831): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
07-02 06:18:08.939: W/System.err(831): at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
07-02 06:18:08.939: W/System.err(831): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
07-02 06:18:08.939: W/System.err(831): at android.view.View.dispatchPointerEvent(View.java:7426)
07-02 06:18:08.949: W/System.err(831): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
07-02 06:18:08.949: W/System.err(831): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
07-02 06:18:08.949: W/System.err(831): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
07-02 06:18:08.949: W/System.err(831): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
07-02 06:18:08.949: W/System.err(831): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
07-02 06:18:08.959: W/System.err(831): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
07-02 06:18:08.959: W/System.err(831): at android.os.MessageQueue.nativePollOnce(Native Method)
07-02 06:18:08.959: W/System.err(831): at android.os.MessageQueue.next(MessageQueue.java:125)
07-02 06:18:08.959: W/System.err(831): at android.os.Looper.loop(Looper.java:124)
07-02 06:18:08.969: W/System.err(831): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-02 06:18:08.969: W/System.err(831): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 06:18:08.969: W/System.err(831): at java.lang.reflect.Method.invoke(Method.java:511)
07-02 06:18:08.969: W/System.err(831): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-02 06:18:08.969: W/System.err(831): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-02 06:18:08.969: W/System.err(831): at dalvik.system.NativeStart.main(Native Method)
那么如何将我的应用程序连接到互联网(我已经设置了权限仍然无法正常工作)!任何帮助表示赞赏!