插件异常发送照片

时间:2013-05-24 09:34:03

标签: android sockets network-programming

我正在使用此代码打开与服务器的连接:

public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {

     BufferedReader in = null;
     try{
         HttpClient client = getHttpClient();
         HttpPost request = new HttpPost(url);
         UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
         request.setEntity(formEntity);
         HttpResponse response = client.execute(request); //The Exception is thrown here
         in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

         StringBuffer sb = new StringBuffer("");
         String line = "";
         String NL = System.getProperty("line.separator");
         while ((line = in.readLine()) != null) {
              sb.append(line + NL);
         }
         in.close();
         String result = sb.toString();

         return result;
     }finally{
         if (in!=null){
             try{
                 in.close();

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

             }
         }
     }
}

发送照片的方法是:

private void enviarImagen(final byte[] byteArray) {
    try {

        Thread thread=new Thread(){
            public void run(){
                try{

                    params.add(new BasicNameValuePair("data", Base64.encodeToString(byteArray, Base64.NO_WRAP)));
                    params.add(new BasicNameValuePair("azafata",General.getUsuario()));
                    params.add(new BasicNameValuePair("caption", filename));
                    params.add(new BasicNameValuePair("id", String.valueOf(id)));
                    CustomHttpClient.executeHttpPost(urlsendfoto, params);

            } catch (ClientProtocolException e) {

                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
                AlertDialog.Builder alert = new AlertDialog.Builder(GpsActivity.this);
                alert.setTitle(R.string.fotografias);
                alert.setMessage(R.string.fotomal);
                alert.setPositiveButton(R.string.accept,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                //Éste intent abre la configuración de geolocalización. 
                                startActivity(new Intent(
                                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                            }
                        });
                alert.setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {

                            }
                        });
                alert.show();
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
        };
        thread.start();         
}finally{

}

}

我第一次发送照片时效果很好。但是,当我拍摄第二个时,它会抛出异常:

05-24 11:22:56.251: W/System.err(11931): java.net.SocketException: Socket closed
05-24 11:22:56.251: W/System.err(11931):    at org.apache.harmony.luni.platform.OSNetworkSystem.read(Native Method)
05-24 11:22:56.259: W/System.err(11931):    at dalvik.system.BlockGuard$WrappedNetworkSystem.read(BlockGuard.java:273)
05-24 11:22:56.259: W/System.err(11931):    at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:458)
05-24 11:22:56.259: W/System.err(11931):    at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:85)
05-24 11:22:56.259: W/System.err(11931):    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
05-24 11:22:56.259: W/System.err(11931):    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
05-24 11:22:56.267: W/System.err(11931):    at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
05-24 11:22:56.267: W/System.err(11931):    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
05-24 11:22:56.267: W/System.err(11931):    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
05-24 11:22:56.275: W/System.err(11931):    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
05-24 11:22:56.275: W/System.err(11931):    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
05-24 11:22:56.275: W/System.err(11931):    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
05-24 11:22:56.275: W/System.err(11931):    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
05-24 11:22:56.282: W/System.err(11931):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:421)
05-24 11:22:56.282: W/System.err(11931):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-24 11:22:56.282: W/System.err(11931):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-24 11:22:56.282: W/System.err(11931):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-24 11:22:56.290: W/System.err(11931):    at com.publidirecta.vinceriazafata.CustomHttpClient.executeHttpPost(CustomHttpClient.java:45)
05-24 11:22:56.290: W/System.err(11931):    at com.publidirecta.vinceriazafata.GpsActivity$9.run(GpsActivity.java:301)

如何解决此错误?更重要的是......为什么会发生?

谢谢。

1 个答案:

答案 0 :(得分:0)

正如堆栈跟踪所示,您在读取响应时出错了。我会说你错误地使用了API。它可能已经读过整个流,它肯定已经关闭了套接字。