2012-10-17 98 views
1

代码: Entir代码在这里上传图像服务器上传图像服务器

static{ 
    httpclient=new DefaultHttpClient(); 
    httpPost=new HttpPost(serverUrl); 
} 

public String uploadImage(String imageFileURL){ 

    Bitmap bitmap=BitmapFactory.decodeFile(imageFileURL); 
    ByteArrayOutputStream baos=new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, baos); 
    byte []ba=baos.toByteArray(); 
    String bal=Base64.encodeBytes(ba); 
    ArrayList<NameValuePair> nameValuePair=new ArrayList<NameValuePair>(); 
    nameValuePair.add(new BasicNameValuePair("image",bal)); 
    String res = ""; 
    StringBuffer buffer = new StringBuffer(); 
    try { 
     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair)); 
     httpresponse=httpclient.execute(httpPost); 
     httpEntity=httpresponse.getEntity(); 
     inputStream=httpresponse.getEntity().getContent(); 

     int contentLength=(int) httpresponse.getEntity().getContentLength(); 
     if(contentLength<0){ 

     } 
     else{ 
      byte []data =new byte[512]; 
      int len=0; 
      try{ 
       while(-1 !=(len=inputStream.read(data))){ 
        buffer.append(new String(data,0,len)); 
       } 

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

    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    finally{ 
     try { 
      inputStream.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    res=buffer.toString(); 
    return res; 
} 

此方法被调用的

onClick(View view){ 
    new Thread(new Runnable(){ 

     public void run() { 
      // TODO Auto-generated method stub 
      response=new ServerCommunication().uploadImage(path); 
     }   
    }).start(); 

但它示出了作为TimeOutException。任何人都可以告诉我做错了什么。

+0

你得到任何错误?如果是,那么请发布相同的堆栈跟踪。你使用任何脚本或其他东西? – juned

回答

0

添加该代码删除超时异常:

final int TIMEOUT_MILLISEC = 20000; 
HttpParams httpParams = new BasicHttpParams(); 
HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC); 
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); 
+0

什么是httpParams .. –

+0

上面的代码是编辑.....只是intialize,http params ...就像那样...我认为它会帮助你.. –

+0

先生我有facinf同样的问题 –