我正在尝试在我这里拥有的在线数据库上发布一个 Put 是我的代码
@Override
public void onRatingChanged(RatingBar arg0, float arg1, boolean arg2)
{
try
{
RatingBar rb = (RatingBar)findViewById(R.id.ratingbar_item);
float currentRating = rb.getRating();
float userRating = arg1;
float count = getIntent().getIntExtra("count", 0);
String newRating = String.valueOf(((currentRating*count)+userRating)/(count+1));
rb.setRating(Float.valueOf(newRating));
String url = "https://api.mongolab.com/api/1/databases/" + getIntent().getStringExtra("State") + "/collections/" + getIntent().getStringExtra("Activity") + "/" + getIntent().getStringExtra("id") +"?apiKey=xxxxxxxxxxxxxxxxxx";
HttpClient http = new DefaultHttpClient();
HttpPut putmethod = new HttpPut(url);
putmethod.setHeader("Content-Type", "application/xml");
String content = "{ \"_id\" : { \"$oid\" : \"" + getIntent().getStringExtra("id") +"\"} , \"name\" : \"" + getIntent().getStringExtra("Name") + "\" , \"address\" : \"" + getIntent().getStringExtra("Address") +"\" , \"rating\" : \" " + newRating + "\" , \"count\" : \""+ String.valueOf(count+1) +"\" , \"coordinates\" : \" " + getIntent().getStringExtra("Coordinates") + "\" , \"img\" : \" "+ getIntent().getStringExtra("imgurl") +" \"}";
putmethod.setEntity(new StringEntity(content, HTTP.UTF_8));
HttpResponse response = http.execute(putmethod);
if (response.getStatusLine().getStatusCode() == 200)
{
Log.i("rating sent", "success");
Toast.makeText(getApplicationContext(), "Feedback submitted, Thank you", Toast.LENGTH_LONG).show();
}
else
{
Log.i("url", url);
Log.i("rating sent", "failed");
Log.i("response", String.valueOf(response.getStatusLine().getStatusCode()));
Log.i("conent", content);
}
}
catch (UnsupportedEncodingException e)
{
Log.i("exception",e.getMessage());
}
catch(ClientProtocolException e)
{
Log.i("exception",e.getMessage());
}
catch (IOException e)
{
Log.i("exception",e.getMessage());
}
}
这是我从 logcat 得到的回复
03-10 05:13:10.489: I/url(5686): https://api.mongolab.com/api/1/databases/california/collections/Hiking/513c72fce4b07fcc2599c604?apiKey=G9Vr2_cykXY9AJvwe9NF_97R-onVVdFj
03-10 05:13:10.489: I/rating sent(5686): failed
03-10 05:13:10.489: I/response(5686): 415
03-10 05:13:10.499: I/conent(5686): { "_id" : { "$oid" : "513c72fce4b07fcc2599c604"} , "name" : "Test" , "address" : "Wild Cat Canyon Rd, El Cajon, CA 92021" , "rating" : " 2.5" , "count" : "1.0" , "coordinates" : " 32.911225,-116.821318" , "img" : " http://s3-media3.ak.yelpcdn.com/bphoto/j7B-K242DfWWFJfCxc7HQg/l.jpg "}
我知道代码 415 是什么意思,但我确实包含了一个标头,明确指出内容将是 application/xml(我也尝试过 txt/xml,它也不起作用)