org.apache.http.conn.HttpHostConnectException:拒绝连接http://10.0.2.2:1232

时间:2016-08-12 10:59:40

标签: java android asp.net-web-api httpclient

我正在尝试使用ASP.Net web api在android中创建用户注册,但是我收到错误" org.apache.http.conn.HttpHostConnectException:与http://10.0.2.2:1232的连接被拒绝"如何解决..下面是代码....请帮帮我

这是我的MainActivity类



class NewUserRegistrationForm extends AsyncTask<String, String, String> {
		@Override
		protected void onPostExecute(String s) {
			progressDialog.dismiss();
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute(); // To change body of overridden methods use
									// File | Settings | File Templates.
			progressDialog = new ProgressDialog(MainActivity.this);
			progressDialog.setMessage("Registration...");
			progressDialog.setCancelable(false);
			progressDialog.show();
			
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			List<NameValuePair> args = new ArrayList<NameValuePair>();
			args.add(new BasicNameValuePair("name", name.getText().toString()));
			args.add(new BasicNameValuePair("email", email.getText().toString()));
			args.add(new BasicNameValuePair("mobile", mobile.getText()
					.toString()));
			args.add(new BasicNameValuePair("address", address.getText()
					.toString()));
			args.add(new BasicNameValuePair("category", category.getText()
					.toString()));
			JSONHttpClient jsonHttpClient = new JSONHttpClient();
			NewUserRegistration newUSerrstrn = (NewUserRegistration) jsonHttpClient
					.PostParams(ServiceUrl.REST_SERVICE_URL, args,
							NewUserRegistration.class);
			Log.d("args", args.toString());

			finish();
			return null;
		}

	}
&#13;
&#13;
&#13;

这是我的JSONHttpClient类

&#13;
&#13;
public class JSONHttpClient {
    public <T> T PostObject(final String url, final T object, final Class<T> objectClass) {
        DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        try {
 
            StringEntity stringEntity = new StringEntity(new GsonBuilder().create().toJson(object));
            httpPost.setEntity(stringEntity);
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");
            httpPost.setHeader("Accept-Encoding", "gzip");
 
            HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                InputStream inputStream = httpEntity.getContent();
                Header contentEncoding = httpResponse.getFirstHeader("Content-Encoding");
                if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                    inputStream = new GZIPInputStream(inputStream);
                }
 
                String resultString = convertStreamToString(inputStream);
                inputStream.close();
                return new GsonBuilder().create().fromJson(resultString, objectClass);
 
            }
 
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (ClientProtocolException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;
    }
 
    public <T> T PostParams(String url, final List<NameValuePair> params, final Class<T> objectClass) {
        String paramString = URLEncodedUtils.format(params, "utf-8");
        url += "?" + paramString;
        return PostObject(url, null, objectClass);
    }
 
    private String convertStreamToString(InputStream inputStream) {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder stringBuilder = new StringBuilder();
        String line = null;
        try {
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
 
        return stringBuilder.toString();
    }
&#13;
&#13;
&#13;

这是我的Web API类

&#13;
&#13;
public class ServiceUrl {
//	 public static final String REST_SERVICE_URL = "http://neta.luspl.com/api/person/PersonInsert/+newUSerrstrn+";
	 
	 public static final String REST_SERVICE_URL = "http://10.0.2.2:1232/api/";
	    public static final String NewUserRegistration = REST_SERVICE_URL + "newUSerrstrn";

}
&#13;
&#13;
&#13;

这是我的LogCat

&#13;
&#13;
08-12 16:19:28.488: W/System.err(2791): org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:1232 refused
08-12 16:19:28.526: I/Choreographer(2791): Skipped 83 frames!  The application may be doing too much work on its main thread.
08-12 16:19:28.589: I/Choreographer(2791): Skipped 75 frames!  The application may be doing too much work on its main thread.
08-12 16:19:28.789: I/Choreographer(2791): Skipped 30 frames!  The application may be doing too much work on its main thread.
08-12 16:19:28.970: I/Choreographer(2791): Skipped 31 frames!  The application may be doing too much work on its main thread.
08-12 16:19:29.207: W/System.err(2791): 	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
08-12 16:19:29.208: W/System.err(2791): 	at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-12 16:19:29.208: W/System.err(2791): 	at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-12 16:19:29.208: W/System.err(2791): 	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-12 16:19:29.208: W/System.err(2791): 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-12 16:19:29.208: W/System.err(2791): 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-12 16:19:29.208: W/System.err(2791): 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-12 16:19:29.209: W/System.err(2791): 	at com.survey.survey.JSONHttpClient.PostObject(JSONHttpClient.java:48)
08-12 16:19:29.209: W/System.err(2791): 	at com.survey.survey.JSONHttpClient.PostParams(JSONHttpClient.java:76)
08-12 16:19:29.209: W/System.err(2791): 	at com.survey.survey.MainActivity$NewUserRegistrationForm.doInBackground(MainActivity.java:107)
08-12 16:19:29.209: W/System.err(2791): 	at com.survey.survey.MainActivity$NewUserRegistrationForm.doInBackground(MainActivity.java:1)
08-12 16:19:29.209: W/System.err(2791): 	at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-12 16:19:29.209: W/System.err(2791): 	at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-12 16:19:29.210: W/System.err(2791): 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-12 16:19:29.218: W/System.err(2791): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-12 16:19:29.219: W/System.err(2791): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-12 16:19:29.237: I/Choreographer(2791): Skipped 50 frames!  The application may be doing too much work on its main thread.
08-12 16:19:29.258: W/System.err(2791): 	at java.lang.Thread.run(Thread.java:818)
08-12 16:19:29.259: W/System.err(2791): Caused by: java.net.ConnectException: failed to connect to /10.0.2.2 (port 1232): connect failed: ETIMEDOUT (Connection timed out)
08-12 16:19:29.265: W/System.err(2791): 	at libcore.io.IoBridge.connect(IoBridge.java:124)
08-12 16:19:29.266: W/System.err(2791): 	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
08-12 16:19:29.266: W/System.err(2791): 	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
08-12 16:19:29.266: W/System.err(2791): 	at java.net.Socket.connect(Socket.java:882)
08-12 16:19:29.266: W/System.err(2791): 	at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
08-12 16:19:29.266: W/System.err(2791): 	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
08-12 16:19:29.267: W/System.err(2791): 	... 16 more
08-12 16:19:29.305: W/System.err(2791): Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
08-12 16:19:29.306: W/System.err(2791): 	at libcore.io.Posix.connect(Native Method)
08-12 16:19:29.306: W/System.err(2791): 	at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
08-12 16:19:29.306: W/System.err(2791): 	at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
08-12 16:19:29.306: W/System.err(2791): 	at libcore.io.IoBridge.connect(IoBridge.java:122)
08-12 16:19:29.307: W/System.err(2791): 	... 21 more
08-12 16:19:29.307: D/args(2791): [name=aa, email=aa, mobile=aa, address=aa, category=aa]
&#13;
&#13;
&#13;

0 个答案:

没有答案