[英]android connection to http refused
I want to send some data to a localhost server in other computer but i couldn't do it. 我想向其他计算机上的本地主机服务器发送一些数据,但是我做不到。 I'm using mac, my firewall is disabled by default. 我使用的是Mac,默认情况下禁用了防火墙。 and i used the other computer's ip address, we're in the same network. 我使用了另一台计算机的IP地址,我们在同一网络中。 I did put in the permissions required. 我确实输入了所需的权限。
This is the logcat message 这是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
This is my code 这是我的代码
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;
}
} }
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在清单文件中授予该权限。
这个许可怎么样
<uses-permission android:name="android.permission.INTERNET" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.