我是原生android开发的新手。我正在使用android studio
进行应用开发。我创建了一个DB
,其中包含两个表格,然后我使用webservice
创建了Yii
。下面是我的DB TABLE
现在我想创建一个应用,当用户输入id
时,应该向用户显示相应的username
。为此,我做了以下
清单
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity
public class MainActivity extends AppCompatActivity {
String URLGET = "http://10.0.2.2:8000/app/web/users/";
String result = "";
TextView getData;
EditText userInput;
public static final String LOG_TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getData = (TextView) findViewById(R.id.getData);
userInput = (EditText) findViewById(R.id.EnterId);
final Button button = (Button) findViewById(R.id.btn_Submit);
button.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
String reqURL = URLGET;
new RestOperation().execute(reqURL);
//callWebService(query);
}
});
}
public class RestOperation extends AsyncTask<String, Void, Void> {
final HttpClient httpClient = new DefaultHttpClient();
String content;
String error;
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
String data = "";
// TextView getData = (TextView) findViewById(R.id.getData);
// EditText userInput = (EditText) findViewById(R.id.EnterId);
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setTitle("Please Wait...");
progressDialog.show();
try {
data += "&" + URLEncoder.encode("data", "UTF-8") + "-" + userInput.getText();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Override
protected Void doInBackground(String... params) {
BufferedReader br = null;
URL url = null;
try {
url = new URL(params[0]);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());
outputStreamWriter.write(data);
outputStreamWriter.flush();
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append(System.getProperty("line.separator"));
}
content = sb.toString();
} catch (MalformedURLException e) {
error = e.getMessage();
e.printStackTrace();
} catch (IOException e) {
error = e.getMessage();
e.printStackTrace();
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
if (error != null) {
getData.setText("Error" + error);
} else {
getData.setText(content);
}
}
}}
现在我在模拟器上运行我的应用程序,这就是我使用10.0.2.2
的原因。但是,当我运行我的应用程序时,我收到以下错误
在logcat
时,我收到了以下警告
java.io.FileNotFoundException: http://10.0.2.2:8000/app/web/users/
02-07 18:27:35.553 14687-14757/com.example.accurat.webservice W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
02-07 18:27:35.553 14687-14757/com.example.accurat.webservice W/System.err: at com.example.accurat.webservice.MainActivity$RestOperation.doInBackground(MainActivity.java:121)
02-07 18:27:35.553 14687-14757/com.example.accurat.webservice W/System.err: at com.example.accurat.webservice.MainActivity$RestOperation.doInBackground(MainActivity.java:75)
02-07 18:27:35.553 14687-14757/com.example.accurat.webservice W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
02-07 18:27:35.553 14687-14757/com.example.accurat.webservice W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-07 18:27:35.554 14687-14757/com.example.accurat.webservice W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-07 18:27:35.554 14687-14757/com.example.accurat.webservice W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-07 18:27:35.554 14687-14757/com.example.accurat.webservice W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-07 18:27:35.554 14687-14757/com.example.accurat.webservice W/System.err: at java.lang.Thread.run(Thread.java:818)
我还在localhost
中使用了my ip
和URL
,但我得到的错误如下所示
我从那天起就坚持下去,真的不知道该怎么做。
感谢任何帮助。