你好朋友我跟随json
{
"category": [{
"id": "90",
"user_id": "1",
"category_id": "27",
"name": "આણંદ કોમર્સિયલ લેયર",
"order": "0",
"created_at": "2014-05-03 17:09:54",
"updated_at": "2014-05-03 17:09:54",
"deleted": "0",
"subtopics": [{
"id": "203",
"user_id": "1",
"category_id": "27",
"subcategory_id": "90",
"name": "આણંદ કોમર્સિયલ લેયર (સંકર જાત)",
"order": "0",
"details": "<p style=\"text-align:justify\"><img alt=\"\" src=\"/packages/wysiwyg/ckeditor/plugins/kcfinder/upload/images/1.png\" style=\"height:271px; width:237px\" /></p>\r\n\r\n<ul>\r\n\t<li style=\"text-align:justify\">પ્રથમ ઈંડું મુક્વાની સરેરાશ ઉંમર:૧૪૨ દિવસ</li>\r\n\t<li style=\"text-align:justify\">સરેરાશ વાર્ષિક ઈંડા ઉત્પાદન : ૩૦૦ ઈંડા</li>\r\n\t<li style=\"text-align:justify\">૪૦ અઠવાડીયાની ઉંમરે ઈંડાનું સરેરાશ વજન : ૫૨ ગ્રામ</li>\r\n\t<li style=\"text-align:justify\">૭૨ અઠવાડીયાની ઉંમરે ઈંડાનું સરેરાશ વજન : ૫૪ ગ્રામ</li>\r\n\t<li style=\"text-align:justify\">સારી જીવાદોરી</li>\r\n</ul>\r\n",
"mobile_detail": "<p style=\"text-align:justify\"><img alt=\"\" src=\"/packages/wysiwyg/ckeditor/plugins/kcfinder/upload/images/1.png\" style=\"height:271px; width:237px\" /></p>\r\n\r\n<ul>\r\n\t<li style=\"text-align:justify\">પ્રથમ ઈંડું મુક્વાની સરેરાશ ઉંમર:૧૪૨ દિવસ</li>\r\n\t<li style=\"text-align:justify\">સરેરાશ વાર્ષિક ઈંડા ઉત્પાદન : ૩૦૦ ઈંડા</li>\r\n\t<li style=\"text-align:justify\">૪૦ અઠવાડીયાની ઉંમરે ઈંડાનું સરેરાશ વજન : ૫૨ ગ્રામ</li>\r\n\t<li style=\"text-align:justify\">૭૨ અઠવાડીયાની ઉંમરે ઈંડાનું સરેરાશ વજન : ૫૪ ગ્રામ</li>\r\n\t<li style=\"text-align:justify\">સારી જીવાદોરી</li>\r\n</ul>\r\n",
"created_at": "2014-05-03 17:11:43",
"updated_at": "2014-05-11 13:41:31",
"deleted": "0",
"images": [],
"videos": []
}]
}]
}
发布方法:
public Object getAppData(GetCategoryData mGetFlag) {
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", StaticValues.mStringEmail));
nameValuePairs.add(new BasicNameValuePair("password", StaticValues.mStringPassword));
nameValuePairs.add(new BasicNameValuePair("id", StaticValues.mStringId));
return postHttpURLWithPostMethodTour(AllUrls.mStringWebApiLink + "api/apps", nameValuePairs, mGetFlag);
}
所以我使用post参数进行服务调用,如:
public Object postHttpURLWithPostMethodTour(String url, List<NameValuePair> nameValuePairs, Object mObject) {
HttpPost httppost;
HttpParams httpParameters;
int timeoutConnection = 60000;
HttpClient httpclient = null;
HttpResponse response = null;
String data = "";
isOtherError = false;
System.out.println("Url " + url);
mFillObject = null;
if (check_Internet()) {
try {
mFillObject = mObject.getClass().newInstance();
URL urlToRequest = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) urlToRequest.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httppost = new HttpPost(url);
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
httppost.addHeader("X-Requested-With", "XMLHttpRequest");
httppost.addHeader("mobile-tokon", StaticValues.mStringMobileToken);
httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
httpclient = new DefaultHttpClient(httpParameters);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
if (nameValuePairs != null) {
for (int index = 0; index < nameValuePairs.size(); index++) {
String paramName = nameValuePairs.get(index).getName();
String paramValue = nameValuePairs.get(index).getValue();
System.out.println("paramName " + paramName);
System.out.println("paramValue " + paramValue);
if (paramName.equalsIgnoreCase("image")) {
if (paramValue.length() > 0) {
System.out.print("File Object found");
entity.addPart(paramName, new FileBody(new File(paramValue)));
}
} else {
entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
}
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
}
// Execute HTTP Post Request
response = httpclient.execute(httppost);
System.out.println("httppost " + httppost.toString());
data = EntityUtils.toString(response.getEntity());
System.out.println("Final Data " + data);
mStringresponse = data;
if (data.equalsIgnoreCase("{\"is_device_deleted\":true}"))
setDeviceToken(true);
mFillObject = mGson.fromJson(data, mFillObject.getClass());
} catch (Exception e) {
isOtherError = true;
}
}
return mFillObject;
}
但是,当我打电话给我时,我得到的输出如下:
httppost org.apache.http.client.methods.HttpPost@5359a64c at httpsost.toString()
所以我知道如何解决这个问题?你的所有建议都很明显。
答案 0 :(得分:0)
为什么要打印出帖子对象?这是没有意义的。您想要打印出结果,而不是post对象。输出的原因与字体无关 - 因为post对象没有更好的toString函数。即使所有结果都是英文的,它也会打印出来。
答案 1 :(得分:0)
试用此代码
private class AysncTaskSend extends AsyncTask<Void,Void,Void>
{
private ProgressDialog regDialog=null;
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try
{
new Thread(new Runnable() {
@Override
public void run() {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("value1",
"Value1"));
postParameters.add(new BasicNameValuePair("value2",
"value2"));
String response = null;
try {
response = SimpleHttpClient
.executeHttpPost("url",
postParameters);
res = response.toString();
return res;
} catch (Exception e) {
e.printStackTrace();
errorMsg = e.getMessage();
}
}
}).start();
try {
Thread.sleep(3000);
// error.setText(resp);
if (null != errorMsg && !errorMsg.isEmpty()) {
}
} catch (Exception e) {
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
// do what u do
}
<强> SimpleHttpClient.java 强>
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
public class SimpleHttpClient {
/** The time it takes for our client to timeout */
public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
/** Single instance of our HttpClient */
private static HttpClient mHttpClient;
/**
* Get our single instance of our HttpClient object.
*
* @return an HttpClient object with connection parameters set
*/
private static HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static String executeHttpatch(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Performs an HTTP GET request to the specified url.
*
* @param url The web address to post the request to
* @return The result of the request
* @throws Exception
*/
public static String executeHttpGet(String url) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
用于部分导入轻松下载
Apache的httpcomponents-httpcore.jar
的HttpClient-4.2.3.jar
添加实体部分