我是本机android开发的新手。我正在研究android studio
。我使用web service api
创建了yii
,我还在mysql
中创建了两个名为Users
和Activity
的表,在User
中输入了一些数据如下表
现在我想要的是,只要用户输入用户的id
,就应该显示特定的username
。
为此,我跟着这个堆栈link并进行了布局
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName|number"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/etId"
style="@style/Widget.AppCompat.EditText" />
<Button
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/btn_Submit"
android:layout_alignBottom="@+id/etId" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="53dp"
android:id="@+id/textView"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:layout_below="@+id/etId"
android:layout_centerHorizontal="true" />
我想在文字上显示名称。
根据我关注的链接但无法查看我所需的结果。
public class MainActivity extends AppCompatActivity {
String URL = "http://localhost:8000/app/web/users/";
String result = "";
TextView tv; public static final String LOG_TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = (EditText)findViewById(R.id.etId);
/*editText.setOnClickListener(new EditText.OnClickListener(){
@Override
public void onClick(View v) {
editText.setText(" ");
}
});
*/
final Button button = (Button)findViewById(R.id.btn_Submit);
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
String query = editText.getText().toString();
callWebService(query);
}
});
}
private void callWebService(String q) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(URL + q);
ResponseHandler<String> handler = new BasicResponseHandler();
try
{
result = httpclient.execute(request, handler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
tv = (TextView)findViewById(R.id.textView);
Toast.makeText(LOG_TAG, "Hi", result).show();
//tv.append("Hi ", result, ":" );
}
更新1
以下是点按按钮时出现的错误
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:500)
at com.example.accurat.webservice.MainActivity$1.onClick(MainActivity.java:53)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
它来自点
`button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
String query = editText.getText().toString();
callWebService(query);
}
});`
更新2
通过以下link我完成了以下
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
现在我的应用程序不会崩溃,但它只显示HI
而不是请求的数据
调试时,我遇到了异常
org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8000 refused
当我在浏览器上提出请求时,它会向我显示所需的结果,如下图所示
注意:
我正在模拟器上测试它。
任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
NetworkOnMainThreadException
。在AsyncTask
或其他Thread
中运行您的代码:
new Thread(new Runnable(){
@Override
public void run() {
// Do network action in this function
}
}).start();
点击NetworkOnMainThreadException了解更多信息。
注 -
简单来说,
不要在UI线程中进行网络工作
但是
如果您使用简单线程,当您从网络响应中获取某些内容并希望在您的视图中显示它时(如在TextView中显示响应消息),则需要返回到UI线程。
如果你不这样做,你将获得ViewRootImpl$CalledFromWrongThreadException
。
所以,对于新手我会建议AssyncTask
。
<强>更新强>
http://localhost
上面的主机已经被运行代码的 模拟器 占用。如果要访问计算机的本地主机,请使用IP地址http://yourip:8080/.
有关详细信息,请参阅此link。
答案 1 :(得分:0)
答案 2 :(得分:0)
Android通常不支持localhost
作为网址,因为大多数计算机都是通过wifi或其他网络连接的。尝试将localhost
替换为您系统的IPADDRESS
。像:
String URL = "http://192.168.1.1:8000/app/web/users/"; //replace 192.168.1.1 with your ip address