我正在尝试在我拥有的在线数据库上发布 这是我的代码
@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,它也没有用)
答案 0 :(得分:0)
您不是在身体中发送XML。 Content-Type应与您实际发送的类型相匹配。如果您要发送JSON(如图所示),您应该设置application / json。如果您实际上打算发送XML,则应将数据包装在XML实体中。