2014-09-22 133 views
0

我想發送一些數據到其他計算機上的本地主機服務器,但我做不到。我使用的是mac,我的防火牆默認是禁用的。我用另一臺電腦的IP地址,我們在同一個網絡。我確實輸入了所需的權限。android連接到http拒絕

這是logcat的消息

09-22 08:06:46.343: D/OpenGLRenderer(1108): Enabling debug mode 0 
09-22 08:07:37.943: W/EGL_genymotion(1108): eglSurfaceAttrib not implemented 
09-22 08:07:41.443: W/EGL_genymotion(1108): eglSurfaceAttrib not implemented 
09-22 08:08:01.379: D/InputStream(1108): Connection to http://172.22.111.225:8080 refused 

這是我的代碼

public class MainActivity extends Activity implements OnClickListener { 

TextView tvIsConnected; 
EditText etName; 
EditText etCountry; 
EditText etTwitter; 
Button btnPost; 

Person person; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // get reference to the views 
    tvIsConnected = (TextView) findViewById(R.id.tvIsConnected); 
    etName = (EditText) findViewById(R.id.etName); 
    etCountry = (EditText) findViewById(R.id.etCountry); 
    etTwitter = (EditText) findViewById(R.id.etTwitter); 
    btnPost = (Button) findViewById(R.id.btnPost); 

    // check if you are connected or not 
    if(isConnected()){ 
     tvIsConnected.setBackgroundColor(0xFF00CC00); 
     tvIsConnected.setText("You are conncted"); 
    } 
    else{ 
     tvIsConnected.setText("You are NOT conncted"); 
    } 

    // add click listener to Button "POST" 
    btnPost.setOnClickListener(this); 

} 

public static String POST(String url, Person person){ 
    InputStream inputStream = null; 
    String result = ""; 

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

    nameValuePairs.add(new BasicNameValuePair("phonenumber",person.getName())); 
    nameValuePairs.add(new BasicNameValuePair("peoplenumber",person.getCountry())); 
    nameValuePairs.add(new BasicNameValuePair("remark",person.getTwitter())); 

    try { 

     // 1. create HttpClient 
     HttpClient httpclient = new DefaultHttpClient(); 

     // 2. make POST request to the given URL 
     HttpPost httpPost = new HttpPost(url); 
     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 



     // 8. Execute POST request to the given URL 
     HttpResponse httpResponse = httpclient.execute(httpPost); 

     // 9. receive response as inputStream 
     inputStream = httpResponse.getEntity().getContent(); 

     // 10. convert inputstream to string 
     if(inputStream != null) 
      result = convertInputStreamToString(inputStream); 
     else 
      result = "Did not work!"; 

    } catch (Exception e) { 
     Log.d("InputStream", e.getLocalizedMessage()); 
    } 

    // 11. return result 
    return result; 
} 

public boolean isConnected(){ 
    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
     if (networkInfo != null && networkInfo.isConnected()) 
      return true; 
     else 
      return false;  
} 
@Override 
public void onClick(View view) { 

    switch(view.getId()){ 
     case R.id.btnPost: 
      if(!validate()) 
       Toast.makeText(getBaseContext(), "Enter some data!", Toast.LENGTH_LONG).show(); 
      // call AsynTask to perform network operation on separate thread 
      new HttpAsyncTask().execute("http://172.22.111.225:8080/OrderServer/orderServer"); 
     break; 
    } 

} 
private class HttpAsyncTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... urls) { 

     person = new Person(); 
     person.setName(etName.getText().toString()); 
     person.setCountry(etCountry.getText().toString()); 
     person.setTwitter(etTwitter.getText().toString()); 

     return POST(urls[0],person); 
    } 
    // onPostExecute displays the results of the AsyncTask. 
    @Override 
    protected void onPostExecute(String result) { 
     Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show(); 
    } 
} 

private boolean validate(){ 
    if(etName.getText().toString().trim().equals("")) 
     return false; 
    else if(etCountry.getText().toString().trim().equals("")) 
     return false; 
    else if(etTwitter.getText().toString().trim().equals("")) 
     return false; 
    else 
     return true;  
} 
private static String convertInputStreamToString(InputStream inputStream) throws IOException{ 
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
    String line = ""; 
    String result = ""; 
    while((line = bufferedReader.readLine()) != null) 
     result += line; 

    inputStream.close(); 
    return result; 

} 

}

+0

您是否能夠從另一個源連接到計算機?目標似乎不接受連接。 – BadSkillz 2014-09-22 08:36:36

回答

0
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

給清單文件的許可。

+0

我確實授予了許可 – NEWBIE 2014-09-22 08:32:37

+0

您是否可以清理項目並重試。這可能只有兩種可能性,一種是沒有互聯網許可,另一種是因爲無法訪問而可能會拒絕URL連接。嘗試從其他瀏覽器中測試該應用的網址。 – Supriya 2014-09-22 08:37:48

0

這個怎麼樣許可

<uses-permission android:name="android.permission.INTERNET" /> 
+0

這不提供問題的答案。要批評或要求作者澄清,在他們的帖子下留下評論 - 你總是可以評論你自己的帖子,一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你會能夠[評論任何帖子](http://stackoverflow.com/help/privileges/comment)。 – XN16 2014-09-22 09:12:40

+0

是的,對不起..我只是想把它放在評論,但我不能因爲聲望點 – 2014-09-23 02:07:53