0

i am making a file and database uploader in android and my upload Code is:

public int uploadFile(ArrayList<String> sourceFileUri, String info, String latitude, String longitude, String id) throws IOException {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost("http://10.0.2.2/deliverysystem/order/add");

            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            entity.addPart("returnformat", new StringBody("json"));
            System.out.println(sourceFileUri.size());
            for(int i=0;i<sourceFileUri.size();i++){
                String sourceFile = sourceFileUri.get(i);
                entity.addPart("uploaded_file"+(i+1), new FileBody(new File(sourceFile)));
            }

            entity.addPart("fld_delivery_id", new StringBody(id));
            entity.addPart("fld_delivery_location", new StringBody(info));
            entity.addPart("fld_latitude", new StringBody(latitude));
            entity.addPart("fld_longitude", new StringBody(longitude));
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost, localContext);....

and when the code reaches last code HttpResponse response = httpClient.execute(httpPost, localContext); it throws an error E/org.apache.http.conn.HttpHostConnectException(4740): Connection to http://10.0.2.2:8080 refused' all the upload task is done but when executing the last code it doesnt return the response code but goes to exception part. i cant understand what is happening can anybody help me?

Here is complete log cat

    E/org.apache.http.conn.HttpHostConnectException(4318): Connection to http://localhost refused
E/org.apache.http.conn.HttpHostConnectException(4318): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
E/org.apache.http.conn.HttpHostConnectException(4318):  at com.example.android.photobyintent.ViewRecipients.uploadFile(ViewRecipients.java:325)
E/org.apache.http.conn.HttpHostConnectException(4318):  at com.example.android.photobyintent.ViewRecipients$1.run(ViewRecipients.java:238)
E/org.apache.http.conn.HttpHostConnectException(4318):  at java.lang.Thread.run(Thread.java:856)
E/org.apache.http.conn.HttpHostConnectException(4318): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
E/org.apache.http.conn.HttpHostConnectException(4318):  at libcore.io.IoBridge.connect(IoBridge.java:114)
E/org.apache.http.conn.HttpHostConnectException(4318):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
E/org.apache.http.conn.HttpHostConnectException(4318):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
E/org.apache.http.conn.HttpHostConnectException(4318):  at java.net.Socket.connect(Socket.java:842)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
E/org.apache.http.conn.HttpHostConnectException(4318):  ... 8 more
E/org.apache.http.conn.HttpHostConnectException(4318): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
E/org.apache.http.conn.HttpHostConnectException(4318):  at libcore.io.Posix.connect(Native Method)
E/org.apache.http.conn.HttpHostConnectException(4318):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
E/org.apache.http.conn.HttpHostConnectException(4318):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
E/org.apache.http.conn.HttpHostConnectException(4318):  at libcore.io.IoBridge.connect(IoBridge.java:112)
E/org.apache.http.conn.HttpHostConnectException(4318):  ... 13 more
4

2 回答 2

0

您是否将其添加到清单文件中?

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

或者网址应该是:

“http://10.0.2.2/deliverysystem/order/add/”

我在网址末尾添加了一个“/”

于 2012-12-31T10:08:18.213 回答
-1

首先将此添加到您的清单文件中

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

然后试试这段代码,

根据您的要求进行一些更改,而不是上面的文件上传放在Asynctask不影响您的应用程序主线程的方法中

protected String doInBackground(String... args) {
    String ServerResponse = "";
    mDbHelper = new DbAdapter(ct);
    mDbHelper.open();
    ImageUploadEntity entity = new ImageUploadEntity();
    List<ImageUploadEntity> imagedatas = mDbHelper
            .fetchImageByModuleId(Submission_id);
    mDbHelper.close();

    Totalfilecount = imagedatas.size();

    if (Totalfilecount != 0) {

        for (int j = 0; j < imagedatas.size(); j++) {
            entity = imagedatas.get(j);
          String path = entity.getFilecontent();

        final HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost("http://10.0.2.2/deliverysystem/order/add");    
         HttpContext localContext = new BasicHttpContext();

            // Indicate that this information comes in parts (text and file)
            final MultipartEntity reqEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            final File file = new File(path);

            if (file.exists()) {
                String filename = Common
                        .MillisecondsToDateFORFileName(System
                                .currentTimeMillis());

    FileBody fileBody = new FileBody(file, "image/png");
    reqEntity.addPart("filecontent", fileBody);
    try {
                    // The rest of the data
     reqEntity.addPart("fld_delivery_id", new StringBody(id));
       reqEntity.addPart("fld_delivery_location", new StringBody(info));
        reqEntity.addPart("fld_latitude", new StringBody(latitude));
        reqEntity.addPart("fld_longitude", new StringBody(longitude));
        reqEntity.addPart("returnformat", new StringBody("json"));
         postRequest.setEntity(reqEntity);

         ServerResponse = httpClient.execute(postRequest,localContext)………

                } catch (Exception e) {
                    // TODO: handle exception

                }
            }

        }

    }
    return ServerResponse;

}
于 2012-12-31T10:05:51.453 回答