2010-11-23 39 views
-2
public class Sendfile extends Activity 
{ 

/** Called when the activity is first created. */ 

public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 

setContentView(R.layout.main); 

String url = "http://http://192.168.0.158:4299"; 

File file = new File(Environment.getExternalStorageDirectory(), 
    "sendingfile.txt"); 

try { 
    HttpClient httpclient = new DefaultHttpClient(); 

HttpPost httppost = new HttpPost(url); 
    InputStreamEntity reqEntity = new InputStreamEntity(
    new FileInputStream(file), -1); 

reqEntity.setContentType("binary/octet-stream"); 

reqEntity.setChunked(true); 

// Send in multiple parts if needed 

httppost.setEntity(reqEntity); 

HttpResponse response = httpclient.execute(httppost); 

Toast.makeText(getApplicationContext(), response.toString(), 
    Toast.LENGTH_LONG).show(); 
    // Do something with response... 
    } catch (Exception e) { 
    // show error 
    } 

} 
} 
+0

我的水晶球在哪裏?我以爲我在那邊......(這是諷刺...我建議你重新考慮你的問題......等等,沒有!) – WarrenFaith 2010-11-23 12:03:53

+0

我無法發送sendfile.txt到服務器..我' m沒有得到任何異常和任何迴應 – reddy 2010-11-23 12:16:00

回答

1

您的第一個問題是,您的網址無效;而不是「http:// http://192.168.0.158:4299」它應該是「http://192.168.0.158:4299」。那裏也可能有更多的問題,但那個問題會跳出頁面。

代碼的風格也非常糟糕。捕獲Exception的大塊代碼幾乎總是不好的。捕獲異常然後忽略它的大塊代碼幾乎總是更糟。