需要帮助我的安卓应用程序(http请求)在模拟器中工作,但在物理星系S5上不起作用



该应用程序在同一台机器上使用模拟器和 xampp 服务器在 android 工作室中运行,但是当我尝试在通过 USB 连接的 Galaxy S5 上运行它并在 sim 上使用我的数据包时,我在日志猫中收到以下错误:(HTTPLog(-静态:isSBSettingEnabled false,据我所知,存在某种连接错误,返回的数组是一个空集, 这会导致应用崩溃。我尝试了以下连接网址,但没有一个有效:http://190.xxx.xx.xx/abcd/search.php,190.xxx.xx.xx/abcd/search.php,http://190.xxx.xx.xx:80/abcd/search.php,190.xxx.xx.xx:80/abcd/search.php

protected String doInBackground(String... params) {
String type = params[0];
// Note Android reserved loopback address is 10.0.2.2
// Testing server
//String login_url = "http://10.0.2.2/ttsrp/search.php";
// Live server
String login_url = "http://190.xxx.xx.xx/abcd/search.php";
Log.i("URL set:",login_url);
if (type.equals("search")){
try {
Log.i("Branch :","Search String Accepted.");
String search_key = params[1];
//String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("search_key", "UTF-8")+"="+
URLEncoder.encode(search_key, "UTF-8");
bufferedWriter.write(post_data);
//Buffer Writer cleanup
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while((line = bufferedReader.readLine())!= null)
bufferedReader.close();{
result += line;
}
inputStream.close();
httpURLConnection.disconnect();
Log.i("Information : ","Close Connection");
return result;
} catch (MalformedURLException e) {
Log.e("ERROR: ","Wrong or malformed URL.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
//alertDialog.setMessage(result);
//alertDialog.show();
ListView listView=(ListView)findViewById(R.id.fp_list);

if (result==null){
Log.e("ERROR : ", "Null Dataset returned.");
}else{
Log.d("Result:", result);
}

JSONArray jarr = null;
JSONObject jobj = null;
try {
jarr = new JSONArray(result);
for (int i=0; i<(jarr.length()); i++){
jobj = jarr.getJSONObject(i);
int id = jobj.getInt("id");
String off = jobj.getString("off");
String act = jobj.getString("act");
String ena = jobj.getString("ena");
int fine = jobj.getInt("fine");
int dem = jobj.getInt("dem");
String lab = "acb " + act + " " + ena + " " +"123 " + dem;
arrayList.add(lab);
arrayList.add(off);
}
//Create Adapter
ArrayAdapter arrayAdapter=new ArrayAdapter(FixedPenalty.this,android.R.layout.simple_list_item_1, arrayList);
//assign adapter to listview
listView.setAdapter(arrayAdapter);
} catch (JSONException e) {
e.printStackTrace();
}

我发现了问题,无线鼠标中的电池电量不足导致此代码意外移动,过早关闭了缓冲区阅读器。

while((line = bufferedReader.readLine())!= null)
bufferedReader.close();{
result += line;
}

应该是

while((line = bufferedReader.readLine())!= null){
result += line;
}
bufferedReader.close();

仍然不知道为什么它仍然在模拟器中工作。

最新更新