数据不插入,结果为空。
在LOG 不会出错得到
在这里发布我的code
的InputStream是= NULL;
字符串结果=;
// HTTP POST
尝试 {
HttpClient的HttpClient的= getNewHttpClient();
最后弦乐体=字符串
.format({\block_face_line \:[{\经度\:-71.34345555,\纬度\:42.7794343},{\经度\:-71.4473179666667,\纬度\:42.7336227666667}, {\经度\:-71.4461721166667,\纬\:42.7321493333333},{\经度\:-71.4473179662267,\纬\:42.726227666667}],\block_face_curb_side \:\左\ ,\block_face_collector_id \:\3 \});
HttpPost httppost =新HttpPost(
https://url.com);
httppost.setHeader(接受,应用/ JSON);
httppost.setHeader(内容类型,
应用/ JSON的;字符集= UTF-8);
httppost.setHeader(内容长度,
Integer.toString(body.length()));
httppost.addHeader(BasicScheme.authenticate(
新UsernamePasswordCredentials(用户名,密码),
UTF-8,假));
httppost.setEntity(新StringEntity(身体,UTF-8));
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
是= entity.getContent();
}赶上(例外五){
Log.e(log_tag,错误的HTTP连接+ e.toString());
}
//转换响应串
尝试 {
的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(
是,ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL){
sb.append(行+\ N);
}
is.close();
结果= sb.toString();
}赶上(例外五){
Log.e(log_tag,错误转换结果+ e.toString());
}
//尝试解析字符串到一个JSON对象
Toast.makeText(getApplicationContext(),结果,Toast.LENGTH_LONG)
。显示();
Log.e(味精,结果);
}
公共HttpClient的getNewHttpClient(){
尝试 {
密钥库的trustStore = KeyStore.getInstance(密钥库
.getDefaultType());
trustStore.load(NULL,NULL);
SSLSocketFactory的SF =新MySSLSocketFactory(的trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
的HttpParams PARAMS =新BasicHttpParams();
HttpProtocolParams.setVersion(参数,可以HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(参数,可以HTTP.UTF_8);
SchemeRegistry注册表=新SchemeRegistry();
registry.register(新计划(HTTP,PlainSocketFactory
.getSocketFactory(),80));
registry.register(新计划(https开头,SF,443));
ClientConnectionManager CCM =新ThreadSafeClientConnManager(
参数,可以登记);
返回新DefaultHttpClient(CCM,则params);
}赶上(例外五){
返回新DefaultHttpClient();
}
}
解决方案
其实我也不怎么看结果
可空
无日志信息。
其他的一些意见:您请求从服务器 UTF-8
的回应,但那么你把它读作 ISO-8859-1
;还有8个字符的缓冲区大小为低得离谱。
Data is not inserted and result is null.
NOT an error get in LOG
Posted here my code
InputStream is = null;
String result = "";
// http post
try {
HttpClient httpclient = getNewHttpClient();
final String body = String
.format("{\"block_face_line\" : [{\"longitude\" : -71.34345555,\"latitude\" : 42.7794343 },{\"longitude\" : -71.4473179666667,\"latitude\" : 42.7336227666667 }, {\"longitude\" : -71.4461721166667,\"latitude\" : 42.7321493333333 },{\"longitude\" : -71.4473179662267,\"latitude\" : 42.726227666667 } ],\"block_face_curb_side\" : \"LEFT\",\"block_face_collector_id\" : \"3\"}");
HttpPost httppost = new HttpPost(
"https://url.com");
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type",
"application/json; charset=utf-8");
httppost.setHeader("Content-length",
Integer.toString(body.length()));
httppost.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("username", "password"),
"UTF-8", false));
httppost.setEntity(new StringEntity(body, "utf-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG)
.show();
Log.e("msg", result);
}
public HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore
.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(
params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
解决方案
Actually I don't see how result
can be null
without a log message.
Some other observations: you request a response in UTF-8
from the server but then you read it as iso-8859-1
; also a buffer size of 8 characters is ridiculously low.