[英]Get username based on user ID using REST api in android studio
我是原生Android開發的新手。 我正在android studio
上工作。 我已經使用yii
創建了一個web service api
,也已經在mysql
創建了兩個名為Users
and Activity
表,並在User
表中輸入了一些數據,如下所示
現在我想要的是每當用戶輸入用戶id
時都應顯示特定的username
。
為此,我遵循了此堆棧鏈接並進行了布局
<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電視; 公共靜態最終字符串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
通過此鏈接,我完成了以下操作
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
當我在瀏覽器上請求時,確實顯示了所需的結果,如下圖所示
注意:
我正在模擬器上測試它。
任何幫助將不勝感激。
NetworkOnMainThreadException
只有當您使用的主要thread.Run你的代碼的Web操作中發生AsyncTask
或其他Thread
:
new Thread(new Runnable(){
@Override
public void run() {
// Do network action in this function
}
}).start();
在NetworkOnMainThreadException上查看更多信息。
注意 -
簡單來說
不要在UI線程中進行網絡工作
但
如果您使用簡單線程,則當您從網絡響應中獲取某些信息並希望在視圖中顯示它(例如TextView中的顯示響應消息)時,您需要返回到UI線程。
如果不這樣做,則將獲得ViewRootImpl$CalledFromWrongThreadException
。
因此,對於新手,我建議使用AssyncTask
。
UPDATE
http://localhost
上面的主機已被運行代碼的仿真器占用。 如果要訪問計算機的本地主機,請使用IP地址作為http://yourip:8080/.
有關更多詳細信息,請參閱此鏈接 。
你應該:
由於大多數計算機都是通過wifi或其他網絡連接的,因此Android通常不支持localhost
作為url。 嘗試將localhost
替換為系統的IPADDRESS
。 喜歡:
String URL = "http://192.168.1.1:8000/app/web/users/"; //replace 192.168.1.1 with your ip address
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.