![](/img/trans.png)
[英]android retrofit consuming asp.net web api [FromBody] always null
[英]Consuming ASP.NET Web Api from Android HttpUrlConnection and Gson
大家好,我想使用Android上的Web服務,我是通過Java Desktop App來完成的,並且運行良好,但是當我在Android上嘗試時,我遇到了很多錯誤。 我已經解決了IIS express的“ localhost”問題。 請幫我。
public void SetCloud(View v) {
ConnectionTask task = new ConnectionTask();
try {
task.execute().get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
Http http = HttpFactory.create(context);
http.post("http://192.168.8.2:57888/api/Employees/AddAccount")
.data(new AccountBag("Test123", "TestServer12","D19916F-7C51-4AD6-AC24"))
.send();*/
}
private class ConnectionTask extends AsyncTask<Void, Void, String>{
String responseCode = null;
@Override
protected String doInBackground(Void... arg0) {
try {
URL url = new URL("http://192.168.8.2:57888/api/Employees/AddAccount");
//URL url = new URL("http://esprit.azurewebsites.net/api/comments");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
AccountBag obj = new AccountBag("Test123", "TestServer12","D19916F-7C51-4AD6-AC24"/*,"DateTime","aaa","bbbb","cccc","dddd","ffff"*/);
Gson gson = new Gson();
// convert java object to JSON format,
// and returned as JSON formatted string
String json = gson.toJson(obj);
OutputStream os = conn.getOutputStream();
//os.write(input.getBytes());
os.write(json.getBytes());
os.flush();
/*if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}*/
responseCode = ""+conn.getResponseCode();
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return responseCode;
}
}
protected void onPostExecute(String result) {
// result is what you got from your connection
//ConnectionTask.responseCode.setText(result);
}
}
public class RestAddGroupUser extends AsyncTask<List<UserGroup>,Void,String> {
private RestService lRestService;
private String url;
group_action activity;
ArrayList<LatLong> mParam;
public RestAddGroupUser(Activity activity) {
url= GlobalData.baseURL + "AddGroupuser";
this.activity = (group_action)activity;
}
protected void onPreExecute(){
}
String result = "";
protected String doInBackground(List<UserGroup>... params) {
try {
StringEntity en;
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
Gson gson = new Gson();
String jsonobj=gson.toJson(params[0]);
jsonobj=jsonobj.substring(1, jsonobj.length()-1);
en= new StringEntity(jsonobj,"UTF-8");
en.setContentType("application/json");
// Add parameters
httppost.setEntity(en);
String jsonobject=gson.toJson(params[1]);
jsonobject=jsonobject.substring(1, jsonobj.length()-1);
en= new StringEntity(jsonobject,"UTF-8");
en.setContentType("application/json");
// Add parameters
httppost.setEntity(en);
httppost = ServiceUtils.SetPostRequestHeader(httppost,activity);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inStream = entity.getContent();
result = convertStreamToString(inStream);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public void onPostExecute(String result){
Gson gson =new Gson();
boolean status = false;
}
public static String convertStreamToString(InputStream is){
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.