有很多与此问题相关的帖子,但我找不到任何答案....无论如何,这里是我的整个活动和清单的代码:
我的本地IP目前是192.168.1.3,我拒绝连接到192.168.1.3但我已经尝试从我的PC和手机中的浏览器访问它,并且本地服务器工作正常。< / p>
package com.cablin.cablinmobile;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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 org.apache.http.params.BasicHttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
public class MainActivity extends Activity {
public static String LoginUrl = "http://192.168.1.3/tspmetodologiaserver/public/mobileauth";
public EditText editTextUsername;
public EditText editTextPassword;
public TextView textViewMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUsername = (EditText) this.findViewById(R.id.editTextUsername);
editTextPassword = (EditText) this.findViewById(R.id.editTextPassword);
textViewMessage = (TextView) this.findViewById(R.id.textViewMessage);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void login(View view) {
new LoginAsyncTask().execute();
/*
Intent intent = new Intent(this, PayPerViewListActivity.class);
intent.putExtra(MessageName, "Something");
this.startActivity(intent);
*/
}
private class LoginAsyncTask extends AsyncTask<String, String, String> {
String result = null;
JSONObject jsonObject = null;
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httpPost = new HttpPost(LoginUrl);
httpPost.setHeader("Content-type", "application/json");
ArrayList<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("username", editTextUsername.getText().toString()));
postParams.add(new BasicNameValuePair("password", editTextPassword.getText().toString()));
InputStream in = null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(postParams));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
in = httpEntity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
result = stringBuilder.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(in != null) in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if(result != null) {
jsonObject = new JSONObject(result);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
if(jsonObject != null)
textViewMessage.setText(jsonObject.toString());
else
textViewMessage.setText("JSON NULL");
}
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cablin.cablinmobile" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<activity
android:name="com.cablin.cablinmobile.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.cablin.cablinmobile.PayPerViewListActivity"
android:label="@string/title_activity_pay_per_view_list" >
</activity>
</application>
</manifest>
这是我的错误:
11-23 20:10:30.626 3373-4336/com.cablin.cablinmobile I/System.out﹕ AsyncTask #4 calls detatch()
11-23 20:10:30.626 3373-4336/com.cablin.cablinmobile W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.3 refused
11-23 20:10:30.626 3373-4336/com.cablin.cablinmobile W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:212)
11-23 20:10:30.626 3373-4336/com.cablin.cablinmobile W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:167)
11-23 20:10:30.626 3373-4336/com.cablin.cablinmobile W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:125)
11-23 20:10:30.626 3373-4336/com.cablin.cablinmobile W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.executeOriginal(DefaultRequestDirector.java:1179)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:644)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at com.cablin.cablinmobile.MainActivity$LoginAsyncTask.doInBackground(MainActivity.java:120)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at com.cablin.cablinmobile.MainActivity$LoginAsyncTask.doInBackground(MainActivity.java:96)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ Caused by: java.net.ConnectException: socket failed: EACCES (Permission denied)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:210)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ ... 15 more
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ Caused by: java.net.SocketException: socket failed: EACCES (Permission denied)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:576)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at java.net.Socket.checkOpenAndCreate(Socket.java:664)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at java.net.Socket.connect(Socket.java:808)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ ... 15 more
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at libcore.io.Posix.socket(Native Method)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:561)
11-23 20:10:30.631 3373-4336/com.cablin.cablinmobile W/System.err﹕ ... 20 more
答案 0 :(得分:3)
“uses-permission”应该在“application”之外
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
...
</application>
我还建议添加
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
因此,您可以验证设备是否具有网络连接
ConnectivityManager connMgr = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
{
...
}