我正在使用android-async-http而且非常喜欢它。我遇到了POST数据的问题。我必须按以下格式将数据发布到API: -
<request>
<notes>Test api support</notes>
<hours>3</hours>
<project_id type="integer">3</project_id>
<task_id type="integer">14</task_id>
<spent_at type="date">Tue, 17 Oct 2006</spent_at>
</request>
根据文档,我尝试使用RequestParams
来做,但它失败了。这是其他任何方式吗?我也可以发布等效的JSON。有什么想法吗?
答案 0 :(得分:124)
Loopj POST示例 - 从他们的Twitter示例扩展:
private static AsyncHttpClient client = new AsyncHttpClient();
通过RequestParams
正常发布:
RequestParams params = new RequestParams();
params.put("notes", "Test api support");
client.post(restApiUrl, params, responseHandler);
发布JSON:
JSONObject jsonParams = new JSONObject();
jsonParams.put("notes", "Test api support");
StringEntity entity = new StringEntity(jsonParams.toString());
client.post(context, restApiUrl, entity, "application/json",
responseHandler);
答案 1 :(得分:22)
@Timothy的回答对我不起作用。
我定义Content-Type
的{{1}}以使其有效:
StringEntity
祝你好运:)
答案 2 :(得分:6)
发布json的更好方法
RequestParams params = new RequestParams();
params.put("id", propertyID);
params.put("lt", newPoint.latitude);
params.put("lg", newPoint.longitude);
params.setUseJsonStreamer(true);
ScaanRestClient restClient = new ScaanRestClient(getApplicationContext());
restClient.post("/api-builtin/properties/v1.0/edit/location/", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
答案 3 :(得分:1)
发布XML
protected void makePost() {
AsyncHttpClient client = new AsyncHttpClient();
Context context = this.getApplicationContext();
String url = URL_String;
String xml = XML-String;
HttpEntity entity;
try {
entity = new StringEntity(xml, "UTF-8");
} catch (IllegalArgumentException e) {
Log.d("HTTP", "StringEntity: IllegalArgumentException");
return;
} catch (UnsupportedEncodingException e) {
Log.d("HTTP", "StringEntity: UnsupportedEncodingException");
return;
}
String contentType = "string/xml;UTF-8";
Log.d("HTTP", "Post...");
client.post( context, url, entity, contentType, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
Log.d("HTTP", "onSuccess: " + response);
}
... other handlers
});
}
答案 4 :(得分:0)
只需将您的xml或json写入字符串并使用正确的标头或不使用发送到服务器。并将“Content-Type”设置为“application / json”
答案 5 :(得分:0)
如果某人遇到httpclient发送为Content-Type: text/plain
的问题,请参阅此链接:https://stackoverflow.com/a/26425401/361100
loopj httpclient有些改变(或有问题),无法覆盖StringEntity
原生内容类型为application/json
。
答案 6 :(得分:0)
您可以将JSON字符串添加为某种类型的InputStream - 我已经使用了ByteArrayStream,然后将其传递给RequestParams,您应该设置correctMimeType
InputStream stream = new ByteArrayInputStream(jsonParams.toString().getBytes(Charset.forName("UTF-8")));
multiPartEntity.put("model", stream, "parameters", Constants.MIME_TYPE_JSON);
答案 7 :(得分:0)
只需创建JSONObject,然后将其转换为String“someData”,然后使用“ByteArrayEntity”发送
private static AsyncHttpClient client = new AsyncHttpClient();
String someData;
ByteArrayEntity be = new ByteArrayEntity(someData.toString().getBytes());
client.post(context, url, be, "application/json", responseHandler);
这对我来说很好。
答案 8 :(得分:0)
将xml文件发布到php服务器:
public class MainActivity extends AppCompatActivity {
/**
* Send xml file to server via asynchttpclient lib
*/
Button button;
String url = "http://xxx/index.php";
String filePath = Environment.getExternalStorageDirectory()+"/Download/testUpload.xml";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
postFile();
}
});
}
public void postFile(){
Log.i("xml","Sending... ");
RequestParams params = new RequestParams();
try {
params.put("key",new File(filePath));
}catch (FileNotFoundException e){
e.printStackTrace();
}
AsyncHttpClient client = new AsyncHttpClient();
client.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes) {
Log.i("xml","StatusCode : "+i);
}
@Override
public void onFailure(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes, Throwable throwable) {
Log.i("xml","Sending failed");
}
@Override
public void onProgress(long bytesWritten, long totalSize) {
Log.i("xml","Progress : "+bytesWritten);
}
});
}
}
将android-async-http-1.4.9.jar添加到android studio后,
去build.gradle并添加:
依赖项下的compile 'com.loopj.android:android-async-http:1.4.9'
在AndroidManifest.xml上添加:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />