AndroidRuntime :: at androID.os.Handler.dispatchMessage(Handler.java:92)
我做错了什么?我该怎么解决这个???
以下是我的课程.
rest.java
public class Rest extends Activity {@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.start_rest); button button = (button) findVIEwByID(R.ID.btn); button.setonClickListener(new VIEw.OnClickListener(){ @OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub String url = "http://192.168.1.145/tst.PHP"; RestService re = new RestService(); JsONObject jb = RestService.doGet(url); TextVIEw tv = (TextVIEw) findVIEwByID(R.ID.txtVIEw); tv.setText(jb.toString()); } }); }}
RestService.java
public class RestService {public static JsONObject doGet(String url) { JsONObject Json = null; httpClIEnt httpclIEnt = new DefaulthttpClIEnt(); // Prepare a request object httpGet httpget = new httpGet(url); // Accept JsON httpget.addheader("accept","application/Json"); // Execute the request httpResponse response; try { response = httpclIEnt.execute(httpget); // Get the response entity // Log.e("myApp","Issue is here...!"); httpentity entity = response.getEntity(); // If response entity is not null if (entity != null) { // get entity contents and convert it to string inputStream instream = entity.getContent(); String result= convertStreamToString(instream); // construct a JsON object with result Json=new JsONObject(result); // Closing the input stream will trigger connection release instream.close(); } } catch (ClIEntProtocolException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (JsONException e) { // Todo auto-generated catch block e.printstacktrace(); } // Return the Json return Json;}private static String convertStreamToString(inputStream is) { BufferedReader br = null; StringBuilder sb = new StringBuilder(); String line; try { br = new BufferedReader(new inputStreamReader(is)); while ((line = br.readline()) != null) { sb.append(line); } } catch (IOException e) { e.printstacktrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printstacktrace(); } } } return sb.toString();}}解决方法 尝试过你发布的代码之后,我得到以下异常(我怀疑你会在logcat中找到相同的东西,如果你看的话)
12-17 10:28:26.850 2182-2182/com.example.app E/AndroIDRuntime﹕ FATAL EXCEPTION: main androID.os.networkonmainthreadException at androID.os.StrictMode$AndroIDBlockGuardPolicy.onNetwork(StrictMode.java:1117) at java.net.InetAddress.lookupHostByname(InetAddress.java:385) at java.net.InetAddress.getAllBynameImpl(InetAddress.java:236) at java.net.InetAddress.getAllByname(InetAddress.java:214) at org.apache.http.impl.conn.DefaultClIEntConnectionoperator.openConnection(DefaultClIEntConnectionoperator.java:137) at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) at org.apache.http.impl.clIEnt.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) at org.apache.http.impl.clIEnt.AbstracthttpClIEnt.execute(AbstracthttpClIEnt.java:555) at org.apache.http.impl.clIEnt.AbstracthttpClIEnt.execute(AbstracthttpClIEnt.java:487) at org.apache.http.impl.clIEnt.AbstracthttpClIEnt.execute(AbstracthttpClIEnt.java:465) at com.example.app.RestService.doGet(RestService.java:31) at com.example.app.MainActivity.onClick(MainActivity.java:28) at androID.vIEw.VIEw.performClick(VIEw.java:4084) at androID.vIEw.VIEw$PerformClick.run(VIEw.java:16966) at androID.os.Handler.handleCallback(Handler.java:615) at androID.os.Handler.dispatchMessage(Handler.java:92) at androID.os.Looper.loop(Looper.java:137) at androID.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method)
你得到这个是因为你试图在主线程上通过网络做某事,这是不允许的. (它在Google design guidelines here中提到)
在这种情况下,最好的解决方案是将服务调用移动到AsyncTask,它在一个单独的线程上运行,不会阻止UI.
试试这个,我换掉了只返回日期的freely available JSON web service的URL
public class MainActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); button button = (button) findVIEwByID(R.ID.btn); button.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { String url = "http://date.Jsontest.com"; new MyAsyncTask().execute(url); } }); } class MyAsyncTask extends AsyncTask<String,VoID,JsONObject> { @OverrIDe protected JsONObject doInBackground(String... urls) { return RestService.doGet(urls[0]); } @OverrIDe protected voID onPostExecute(JsONObject JsonObject) { TextVIEw tv = (TextVIEw) findVIEwByID(R.ID.txtVIEw); tv.setText(JsonObject.toString()); } }}总结
以上是内存溢出为你收集整理的无法在android中调用REST API全部内容,希望文章能够帮你解决无法在android中调用REST API所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)