我正在创建一个Android应用程序,允许用户通过在所述服务器上运行php脚本将他们希望销售的项目上传到服务器我通过namevaluepair将参数传递给PHP脚本
但我收到的错误是
- 未处理的异常类型IOException
- 未处理的例外类型
- ClientProtocolException
在我尝试向数组添加更多参数之后出现这些错误
Btn_Submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
byte[] data;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
InputStream inputStream;
List<NameValuePair> nameValuePairs;
String ItemDescription = TxtItemDescription.getText().toString();
String ItemTitle = TxtTitle.getText().toString();
String StartTimeAndDate = TxtStartTimeAndDate.getText().toString();
String StartPrice = TxtStartPrice.getText().toString();
class MyAsyncTask extends AsyncTask<String, Integer, Double>{
protected Double doInBackground(String... params){
return (Double) null;
}
protected void onPostExecute(Double result){
Toast.makeText(getApplicationContext(), "Data Sent", Toast.LENGTH_LONG).show();
httpclient = new DefaultHttpClient();
httppost = new HttpPost(
"http://www.albarrett.co.uk/PS_Auctions/test.php");
// Add your data
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", ItemDescription));
params.add(new BasicNameValuePair("price", ItemTitle));
params.add(new BasicNameValuePair("description", StartTimeAndDate));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
inputStream = response.getEntity().getContent();
data = new byte[256];
buffer = new StringBuffer();
int len = 0;
while (-1 != (len = inputStream.read(data))) {
buffer.append(new String(data, 0, len));
}
inputStream.close();
}
}
});
答案 0 :(得分:0)
未处理并不一定意味着抛出错误,而是在抛出错误时需要执行某些操作。
您可以使用try / catch执行此操作。如果你使用eclipse,你可以点击错误并自动应用它,当然你也可以手动执行:
try {
.. your code that might throw something ..
} catch(IOException ex) {
//writes to your log
ex.printStackTrace();
}
您可以将IOException
替换为任何其他例外,或使用Exception
来捕获任何例外。
注意:顺便说一句,如果您这样做,请阻止主线程上的网络连接。