android - 如何使用 HTTP-response-handler 接收 php 响应数组

标签 android httprequest httpresponse requesthandler

在我的新 Android 程序中,我需要向 PHP 页面发送一个请求并接收作为数组的响应。为此,我创建了一个包含 http 请求和响应功能的类。它将发送请求并接收一些响应,但我无法打印这些数组,而且我不知道它是作为数组还是单个字符串出现。任何人请帮我修复这个错误。这是我的课...

public class HistoryActivity extends Activity implements LocationListener{

    String p_u_name;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    List<NameValuePair> nameValuePairs;
    ProgressDialog dialog = null;
    double park_latitude;
    double park_longitude;
    LocationManager locManager;
    HttpEntity entity;
    String rep;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);

        SharedPreferences prefs = getSharedPreferences("myprefs", 0);

        p_u_name = prefs.getString("KEY_USERNAME", "");

              System.out.println("your name is"+p_u_name);

                locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, this);
                //get the current location (last known location) from the location manager
                Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                if(location!=null)
                {
                park_latitude = location.getLatitude();
                park_longitude = location.getLongitude();
                dialog = ProgressDialog.show(HistoryActivity.this, "", 
                        "Validating user...", true);
                 new Thread(new Runnable() {
                        public void run() {
                            parking();                          
                        }
                      }).start(); 

                }
                else
                {
                    Toast.makeText(HistoryActivity.this,"no location found", Toast.LENGTH_SHORT).show();    
                }

        }
    public void parking(){
         try{  
             httpclient=new DefaultHttpClient();
             httppost= new HttpPost("http://10.0.2.2/test_login/history.php");

             nameValuePairs = new ArrayList<NameValuePair>(2);

             nameValuePairs.add(new BasicNameValuePair("username",p_u_name.trim()));
             nameValuePairs.add(new BasicNameValuePair("park_latitude",Double.toString(park_latitude).trim()));
             nameValuePairs.add(new BasicNameValuePair("park_longitude",Double.toString(park_longitude).trim())); 
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
             ResponseHandler<String> responseHandler = new BasicResponseHandler();


             String[] response =new String[2];

             for(int i=0; i<10; i++){
             response [i]= httpclient.execute(httppost, responseHandler);
             System.out.println("Response : " +response[i]); 
             runOnUiThread(new Runnable() {
                 public void run() {

                     dialog.dismiss();
                 }


             });
             }

int d=response.length;

System.out.println(d);


             if(d<1){
                 runOnUiThread(new Runnable() {
                     public void run() {
                         Toast.makeText(HistoryActivity.this,"Successfully parked", Toast.LENGTH_SHORT).show();

                     }
                 });

//               startActivity(new Intent(LoginActivity.this, MainActivity.class));
             }
             else{
                 Toast.makeText(HistoryActivity.this,"Message", Toast.LENGTH_SHORT).show();
                 showAlert();                
             }

         }catch(Exception e){
             dialog.dismiss();
             System.out.println("Exception : " + e.getMessage());
         }
     }
     public void showAlert(){
         HistoryActivity.this.runOnUiThread(new Runnable() {
             public void run() {
                 AlertDialog.Builder builder = new AlertDialog.Builder(HistoryActivity.this);
                 builder.setTitle("Parking Error.");
                 builder.setMessage("Try again?.")  
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent r = new Intent(getApplicationContext(),HistoryActivity.class);
                                startActivity(r);
                            }
                        });                     
                 AlertDialog alert = builder.create();
                 alert.show();               
             }
         });

     }
    public void onLocationChanged(Location arg0) {
        // TODO Auto-generated method stub

    }
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}

最佳答案

尝试更改本地主机的地址。我没有看到任何其他错误。

关于android - 如何使用 HTTP-response-handler 接收 php 响应数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13133686/

相关文章:

python - 如果网络服务器返回错误,但仅在上传大文件时,请求库会引发 ConnectionError

javascript - Chrome 中的 view-response.js 有什么作用?

java - setResult 不适用于@EActivity

java - 使用 Intent 开始上一个 Activity ,而不创建新 Activity

android - 从包中发布的错误通知无法展开 RemoteViews

java - 记录 http 请求获取/发布参数的最佳方式(且快速)

javascript - 了解 Express.js 中间件优先级

Android:设置一个按钮做几件事

vb.net - HttpUtility.ParseQueryString 未按预期工作

.net - 在 Silverlight 中使用 HttpResponse 上传数据