httpClientpost 방법 분석 json 데이터(서버에 전달, 서버에 전달)
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
public class json extends Activity {
public Context context;
private TextView textView1;
public static String URL = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
private DefaultHttpClient httpClient;
StringBuilder result = new StringBuilder();
private static final int TIMEOUT = 60;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HttpParams paramsw = createHttpParams();
httpClient = new DefaultHttpClient(paramsw);
HttpPost post = new HttpPost(
"http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", "this is post"));
try {
// json
JSONObject json = new JSONObject();
Object email = null;
json.put("email", email);
Object pwd = null;
json.put("password", pwd);
StringEntity se = new StringEntity( "JSON: " + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(post);
int httpCode = httpResponse.getStatusLine().getStatusCode();
if (httpCode == HttpURLConnection.HTTP_OK&&httpResponse!=null) {
Header[] headers = httpResponse.getAllHeaders();
HttpEntity entity = httpResponse.getEntity();
Header header = httpResponse.getFirstHeader("content-type");
// json ( json )
InputStream inputStream = entity.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);// 。
String s;
while (((s = reader.readLine()) != null)) {
result.append(s);
}
reader.close();//
// result JSONObject。 。
JSONObject jsonObject = new JSONObject(result.toString());
String re_username = jsonObject.getString("username");
String re_password = jsonObject.getString("password");
int re_user_id = jsonObject.getInt("user_id");
setTitle(" id_"+re_user_id);
Log.v("url response", "true="+re_username);
Log.v("url response", "true="+re_password);
} else {
textView1.setText("Error Response" + httpResponse.getStatusLine().toString());
}
} catch (UnsupportedEncodingException e) {
} catch (ClientProtocolException e) {
} catch (IOException e) {
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();// 。
httpClient = null;
}
}
}
public static final HttpParams createHttpParams() {
final HttpParams params = new BasicHttpParams();
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setConnectionTimeout(params, TIMEOUT * 1000);
HttpConnectionParams.setSoTimeout(params, TIMEOUT * 1000);
HttpConnectionParams.setSocketBufferSize(params, 8192 * 5);
return params;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.