2010-11-23 54 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的大块代码几乎总是不好的。捕获异常然后忽略它的大块代码几乎总是更糟。

相关问题