problem on sending file to server in android..formated code
public class Sen开发者_开发百科dfile 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
}
}
}
Your first issue is that your URL is invalid; instead of "http://http://192.168.0.158:4299" it should read "http://192.168.0.158:4299". There may well be more problems in there as well, but that one leaps off the page.
The style of the code is pretty horrible as well. A large block of code that catches Exception is almost always bad. A large block of code that catches Exception and then ignores it is almost always worse.
精彩评论