2011-12-01 88 views
0

我想从android应用程序(带模拟器)到现在的Web服务器(一个PHP页面)发送数据。首先,我曾用模拟器尝试过这个程序,它正在工作。之后,我用电话尝试了这个程序,发生了一个异常:从Android发送数据到PHP页面

- > IO异常:操作超时。我的代码

第一部分:

HttpClient httpclient = new DefaultHttpClient(); 

HttpPost httppost = new HttpPost("http://10.0.2.2:90/takeDatas.php"); 
try {     
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("enlem", latitude)); 
    nameValuePairs.add(new BasicNameValuePair("boylam", longitude)); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    httpclient.execute(httppost); 

} catch (ClientProtocolException e) { 
    Toast.makeText(ReportLocationActivity.this, "Client protokol exception ", Toast.LENGTH_LONG).show(); 
} catch (IOException e) { 
    Toast.makeText(ReportLocationActivity.this, "IO exception "+e.getMessage(), Toast.LENGTH_LONG).show(); 
} 

我希望你能帮助我。 在此先感谢。

+0

你能使用相同的URL从浏览器连接吗? – Guillaume

+0

您是否在Android清单中添加了互联网连接权限? – jcfrei

+0

我认为android模拟器是一个独立的虚拟机,它不能到达本地主机域..作为Guillaurne建议,尝试从你的模拟器网页浏览器到这个网址来缩小你的问题(如果是正确的是没有代码相关..) – Joe

回答

2

仿真器中的localhost是仿真器本身。不知道Windows环境,但在Linux下,我能够通过IP 10.0.2.2从仿真器访问主机系统上的Web服务器(注意:该IP不是我的Linux系统的IP,而是来自仿真器,它完全可访问由此知识产权)。 您可以阅读更多关于仿真器网络here

+0

是的,我用10.0.2.2:90/simpleSending.php,没有连接错误。谢谢.. Php错误仍然存​​在:“注意:未定义的索引:在第3行C:\ wamp \ www \ simpleSending.php中的操作” – iremce

+0

您的php问题是否已解决 – mH16

1

是的,而不是使用127.0.0.1或http://localhost:90,你必须使用http://10.0.2.2/simpleSending.php

HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://10.0.2.2/yourpage.php"); 

    //This is the data to send 
    String MyName = "flower"; //any data to send 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
     nameValuePairs.add(new BasicNameValuePair("action", MyName)); 

     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 

     ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
     String response = httpclient.execute(httppost, responseHandler); 


     //This is the response from a php application 
     String reverseString = response; 
     Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show(); 

    } catch (ClientProtocolException e) { 
     Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show(); 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show(); 
     // TODO Auto-generated catch block 
    } 
+0

如果您将使用http://10.0.2.2 :90/yourpage.php,没有任何反应。相反,你使用http://10.0.2.2/yourpage.php – mH16

+0

如果我使用10.0.2.2/mypage.php,模拟器屏幕上显示错误消息:CPE response org .apache.http.client.HttpResponseException:未找到 – iremce

+0

首先让你的php代码$ _REQUEST,它将支持get和post。并转到浏览器并检查它是否像这样http://localhost/yourpage.php?action = hussain。有了这个,http://10.0.2.2/yourpage.php,其在这里的工作很好 – mH16

1

这个错误可能会发生,因为异步方法的call.So你需要用的是异步方法的类。 这里是代码:

package com.example.asynchttppost; 

    import java.io.IOException; 
    import java.util.ArrayList; 
    import java.util.List; 

import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 

    import android.app.Activity; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.ProgressBar; 
    import android.widget.Toast; 

    public class MainActivity extends Activity implements OnClickListener{ 

     private EditText value; 
     private Button btn; 
     private ProgressBar pb; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      value=(EditText)findViewById(R.id.editText1); 
      btn=(Button)findViewById(R.id.button1); 
      pb=(ProgressBar)findViewById(R.id.progressBar1); 
      pb.setVisibility(View.GONE); 
      btn.setOnClickListener(this); 
     } 


     public void onClick(View v) { 
      // TODO Auto-generated method stub 
       if(value.getText().toString().length()<1){ 

        // out of range 
        Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show(); 
       }else{ 
        pb.setVisibility(View.VISIBLE); 
        new MyAsyncTask().execute(value.getText().toString());  
       } 


     } 

     private class MyAsyncTask extends AsyncTask<String, Integer, Double>{ 

      @Override 
      protected Double doInBackground(String... params) { 
       // TODO Auto-generated method stub 
       postData(params[0]); 
       return null; 
      } 

      protected void onPostExecute(Double result){ 
       pb.setVisibility(View.GONE); 
       Toast.makeText(getApplicationContext(), "Code Sent", Toast.LENGTH_SHORT).show(); 
      } 
      protected void onProgressUpdate(Integer... progress){ 
       pb.setProgress(progress[0]); 
      } 

      public void postData(String valueIWantToSend) { 
       // Create a new HttpClient and Post Header 
       String url= "your website url"; 
       //String url ="http://10.0.2.2/remoteaccess/"; 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(url); 

       try { 
        // Add your data 
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
        nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend)); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

        // Execute HTTP Post Request 
        HttpResponse response = httpclient.execute(httppost); 

       } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
       } 
      } 

     } 
    }