需要帮助,我的安卓应用(http请求)在模拟器中工作,但在实体Galaxy S5上却不能工作。

问题描述 投票:-2回答:1

该应用在android studio中使用模拟器和xampp服务器在同一台机器上工作,但是当我尝试在通过usb连接的Galaxy S5上运行它并在模拟器上使用我的数据包时,我在logcat中得到以下错误。(HTTPLog)-Static: isSBSettingEnabled false, 从我的判断来看,存在某种连接错误,返回的数组是一个空集,这导致应用程序崩溃。我尝试了以下连接URS,但都没有成功。http:/190.xxx.xx.xx.xxabcdsearch.php。,190.xxx.xx.xx.xxabcdsearch.php。http:/190.xxx.xx.xx.xx:80abcdsearch.php。,190.xxx.xx.xx.xx:80abcdsearch.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();
        }
android http android-asynctask
1个回答
0
投票

我找到了问题所在,无线鼠标的电池电量过低,导致这段代码意外移动,过早的关闭了bufferreader。

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

应该是

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

不过还是不知道为什么在模拟器中还能用。

© www.soinside.com 2019 - 2024. All rights reserved.