HttpPost不在主要的外面工作

时间:2014-06-14 03:50:26

标签: java android android-asynctask http-post

首先,对不起我的英语,还没掌握这门语言。 其次,我正在开发一个Web服务器,它向服务器发出请求并遇到问题。

当我调用一个名为" last_url"的函数时从主要的HttpPost我运行一个没有问题的电话。但是,如果我在经历了两次从服务器收集数据并执行不同的http请求的活动之后运行,我就不会正确运行HttpPost。

任何人都可以伸出援手吗?我真的坚持这个,无法找到解决问题的方法。

谢谢。

跑步前打电话:

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;

    import org.apache.commons.lang3.StringEscapeUtils;
    import org.apache.commons.lang3.text.translate.UnicodeUnescaper;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.StatusLine;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;

    import android.os.AsyncTask;


    public class PeticionWeb extends AsyncTask<String, String, String>{


        protected String doInBackground(String... uri) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = null;
            String responseString = null;
            try {


           response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
                out.close();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        //Parseamos a unicode sin problema y eliminamos   \\.
        UnicodeUnescaper     unescaper     = new UnicodeUnescaper();
        responseString = unescaper.translate(responseString);
        responseString = StringEscapeUtils.unescapeJava(responseString);
        //System.out.println(responseString);

        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {

        super.onPostExecute(result);
        //Do anything with response..
    }
    protected void onPreExecute() {



    }


   }

通话失败:

import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; 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 android.os.AsyncTask; import android.util.Log; public class PeticionWeb extends AsyncTask<String, Void, String> //params,progres,result { @Override protected String doInBackground(String... params) { BufferedReader inBuffer = null; HttpClient httpClient =null; HttpPost request = null; List<NameValuePair> postParameters = null; String url = params[0].toString().trim(); String result = "fail"; try { httpClient = new DefaultHttpClient(); request = new HttpPost(url); postParameters = new ArrayList<NameValuePair>(); String[] a_param = params[1].split(","); String[] a_values = params[2].split(","); for(int i = 0; i < a_param.length;i++){ postParameters.add( new BasicNameValuePair(a_param[i],a_values[i]) ); System.out.println("Params1: "+a_param[i]+"="+a_values[i]); } UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); request.setEntity(formEntity); HttpResponse respuesta = httpClient.execute(request); ByteArrayOutputStream out = null; out = new ByteArrayOutputStream(); respuesta.getEntity().writeTo(out); out.close(); result = out.toString(); } catch(Exception e) { Log.e("POST",e.toString()); result = e.getMessage(); } finally { if (inBuffer != null) { try { inBuffer.close(); } catch (IOException e) { Log.e("FalloBuffer: ",e.toString()); e.printStackTrace(); } } } return result; } protected void onPostExecute(String page) { } } </pre></code>

0 个答案:

没有答案