确保在分配另一个连接之前释放连接

时间:2014-05-04 21:13:22

标签: java android mysql multithreading httprequest

我正在尝试MultiThreading并同时向android发送4个请求到php文件但是我遇到了这个问题 确保在分配另一个连接之前释放连接。 如何解决呢

这是我的连接代码

   package system.registrationsystem;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

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.json.JSONObject;

import android.util.Log;

public class JSONParser {



    JSONObject jobj=null;
    InputStream is=null;
    static String json="";
    DefaultHttpClient httpClient=null;
    HttpPost httpPost=null;


public JSONParser(){    
}
public void OpenConnection(String url){
     httpClient=new DefaultHttpClient();
 httpPost=new HttpPost(url);


}

public void InsertDataIntoServer(List<NameValuePair> parm){
    StringBuilder sb=null;
    HttpResponse response=null;
    HttpEntity entity=null;
    try{

        Runtime.getRuntime().gc();

        httpPost.setEntity(new UrlEncodedFormEntity(parm));
         response=httpClient.execute(httpPost);

        entity=response.getEntity();
        is=entity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }catch (ClientProtocolException e) {
        e.printStackTrace();
    }
catch (IOException e) {
        e.printStackTrace();
    }

    try{
        BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        sb=new StringBuilder();
        String line=null;
    while( (line=reader.readLine()) !=null ){
        sb.append(line+"\n");
    }


    is.close();
    response.getEntity().consumeContent();

    Log.v("Return SB",sb.toString());

    }catch(Exception e){
    e.printStackTrace();
    }

}

}

0 个答案:

没有答案