需要帮助我的 android 应用程序(http 请求)在模拟器中工作,但不在物理 Galaxy S5 上
Posted
技术标签:
【中文标题】需要帮助我的 android 应用程序(http 请求)在模拟器中工作,但不在物理 Galaxy S5 上【英文标题】:Need help my android app (http request) is working in an emulator but not on a physical galaxy S5 【发布时间】:2020-09-21 13:09:55 【问题描述】:该应用程序在 android studio 中使用模拟器和 xampp 服务器在同一台机器上运行,但是当我尝试在通过 USB 连接的 Galaxy S5 上运行它并在 sim 上使用我的数据包时,我在 logcat 中收到以下错误: (HTTPLog)-Static: 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();
【问题讨论】:
执行此操作时 Galaxy S5 是否与计算机连接到同一网络? 没有断开wifi 【参考方案1】:我发现了问题,无线鼠标的电池电量不足导致此代码意外移动,过早关闭缓冲区读取器。
while((line = bufferedReader.readLine())!= null)
bufferedReader.close();
result += line;
应该是
while((line = bufferedReader.readLine())!= null)
result += line;
bufferedReader.close();
仍然不知道为什么它仍然在模拟器中工作。
【讨论】:
以上是关于需要帮助我的 android 应用程序(http 请求)在模拟器中工作,但不在物理 Galaxy S5 上的主要内容,如果未能解决你的问题,请参考以下文章
我需要帮助在我的 android 应用程序中正确显示 json 数据