我是Android新手。我想要做的是当我按下按钮并在文本字段中写入字符串时,通过Web服务获取String。
Manifext.xml:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AndroidClientActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="Button" />
</LinearLayout>
的strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<string name="hello">Hello World, AndroidClientActivity!</string>
<string name="app_name">AndroidClient</string>
<string name="button">Message</string>
<string name="myClickHandler">myClickHandler</string>
AndroidClientActivity.class:
package com.maze.client;
import javax.ws.rs.core.MediaType;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
public class AndroidClientActivity extends Activity {
private EditText text;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(R.id.editText1);
}
public void myClickHandler(View view){
switch (view.getId()) {
case R.id.button1:
WebResource wbr;
Client client = Client.create();
wbr = client.resource("http://my.ip.address:8080/MazeService/rest/service/hello");
String result = wbr.queryParam("number", "10").accept(MediaType.APPLICATION_JSON).get(String.class);
text.setText(result);
break;
}
}
}
这不起作用。我在LogCat中收到此错误(请帮我编辑一下):
02-08 23:39:04.423: E/AndroidRuntime(660): FATAL EXCEPTION: main
02-08 23:39:04.423: E/AndroidRuntime(660): java.lang.IllegalStateException: Could not execute method of the activity
02-08 23:39:04.423: E/AndroidRuntime(660): at android.view.View$1.onClick(View.java:3044)
02-08 23:39:04.423: E/AndroidRuntime(660): at android.view.View.performClick(View.java:3511)
02-08 23:39:04.423: E/AndroidRuntime(660): at android.view.View$PerformClick.run(View.java:14105)
02-08 23:39:04.423: E/AndroidRuntime(660): at android.os.Handler.handleCallback(Handler.java:605)
02-08 23:39:04.423: E/AndroidRuntime(660): at android.os.Handler.dispatchMessage(Handler.java:92)
02-08 23:39:04.423: E/AndroidRuntime(660): at android.os.Looper.loop(Looper.java:137)
02-08 23:39:04.423: E/AndroidRuntime(660): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-08 23:39:04.423: E/AndroidRuntime(660): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 23:39:04.423: E/AndroidRuntime(660): at java.lang.reflect.Method.invoke(Method.java:511)
02-08 23:39:04.423: E/AndroidRuntime(660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-08 23:39:04.423: E/AndroidRuntime(660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-08 23:39:04.423: E/AndroidRuntime(660): at dalvik.system.NativeStart.main(Native Method)
02-08 23:39:04.423: E/AndroidRuntime(660): Caused by: java.lang.reflect.InvocationTargetException
02-08 23:39:04.423: E/AndroidRuntime(660): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 23:39:04.423: E/AndroidRuntime(660): at java.lang.reflect.Method.invoke(Method.java:511)
02-08 23:39:04.423: E/AndroidRuntime(660): at android.view.View$1.onClick(View.java:3039)
02-08 23:39:04.423: E/AndroidRuntime(660): ... 11 more
02-08 23:39:04.423: E/AndroidRuntime(660): Caused by: com.sun.jersey.api.client.ClientHandlerException: android.os.NetworkOnMainThreadException
02-08 23:39:04.423: E/AndroidRuntime(660): at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
02-08 23:39:04.423: E/AndroidRuntime(660): at com.sun.jersey.api.client.Client.handle(Client.java:648)
02-08 23:39:04.423: E/AndroidRuntime(660): at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670)
02-08 23:39:04.423: E/AndroidRuntime(660): at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
02-08 23:39:04.423: E/AndroidRuntime(660): at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:503)
02-08 23:39:04.423: E/AndroidRuntime(660): at com.maze.client.AndroidClientActivity.myClickHandler(AndroidClientActivity.java:32)
02-08 23:39:04.423: E/AndroidRuntime(660): ... 14 more
02-08 23:39:04.423: E/AndroidRuntime(660): Caused by: android.os.NetworkOnMainThreadException
02-08 23:39:04.423: E/AndroidRuntime(660): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.io.IoBridge.connect(IoBridge.java:112)
02-08 23:39:04.423: E/AndroidRuntime(660): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
02-08 23:39:04.423: E/AndroidRuntime(660): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
02-08 23:39:04.423: E/AndroidRuntime(660): at java.net.Socket.connect(Socket.java:842)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
02-08 23:39:04.423: E/AndroidRuntime(660): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
02-08 23:39:04.423: E/AndroidRuntime(660): at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:240)
02-08 23:39:04.423: E/AndroidRuntime(660): at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147)
02-08 23:39:04.423: E/AndroidRuntime(660): ... 19 more
02-08 23:39:08.343: I/Process(660): Sending signal. PID: 660 SIG: 9
答案 0 :(得分:17)
在Android 3.x及更高版本中埋藏在堆栈跟踪中为Caused by: com.sun.jersey.api.client.ClientHandlerException: android.os.NetworkOnMainThreadException
,您无法在主线程上执行网络I / O(这是您正在尝试执行的操作)。您需要使用AsyncTask进行网络请求。
答案 1 :(得分:0)
我遇到了同样的问题并且发疯了。
只有您必须在引发错误的活动的onCreate
方法的第一行中添加此代码! (super.onCreate
之前):
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);