Android Studio java。伊奥。使用web服务时发生FileNotFoundException
我是原生安卓开发的新手。我正在使用安卓 studio
开发我的应用程序。我创建了一个包含两个表的DB
,然后使用Yii
创建了一个webservice
。下面是我的DB表
现在我想做一个应用程序,当用户输入id
时,相应的username
应该显示给用户。为此,我做了以下工作
清单
<uses-permission 安卓:name="安卓.permission.INTERNET"/>
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:supportsRtl="true"
安卓:theme="@style/AppTheme">
<activity 安卓:name=".MainActivity">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
main活动
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);
}
}
}}
现在我在emulator上运行我的应用程序,所以我使用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.安卓.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 安卓.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 安卓.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)
我也在URL
中使用了localhost
和my ip
,但我得到的错误如下所示
我从那天起就一直坚持着,真的不知道该怎么办
感谢您的帮助
共 (0) 个答案