我必须制作一个“创建用户帐户”的东西。 我必须将json发送到服务器并检索另一个json,这就是结果。 那么如何将json发送到服务器并重新启动另一个json?
我已经构建了必须发送的json对象。
贝娄是我目前的尝试,基于我所学到的但我还没有。
我发送的json名为userCreate
,url字符串名为path
。
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Thread t = new Thread() {
public void run() {
Looper.prepare(); //For Preparing Message Pool for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
try {
HttpPost post = new HttpPost(path);
StringEntity se = new StringEntity( userCreate.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/*Checking response */
if(response!=null){
InputStream in = response.getEntity().getContent(); //Get the data in the entity
Toast.makeText(ctx, in.toString(), 2000).show();
}
} catch(Exception e) {
e.printStackTrace();
Toast.makeText(ctx, "nu merge", 2000).show();
}
}
};
t.start();
}
});
09-17 13:45:29.337: D/request(5838): {"request":{"action":"createUser","user":{"last_name":"asd","first_name":"asd","username":"asd","password":"d41d8cd98f00b204e9800998ecf8427e","email":"asd@yahoo.com"}}}
09-17 13:45:29.337: W/System.err(5838): android.os.NetworkOnMainThreadException
09-17 13:45:29.337: W/System.err(5838): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-17 13:45:29.337: W/System.err(5838): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-17 13:45:29.337: W/System.err(5838): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-17 13:45:29.337: W/System.err(5838): at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-17 13:45:29.337: W/System.err(5838): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-17 13:45:29.337: W/System.err(5838): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-17 13:45:29.337: W/System.err(5838): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-17 13:45:29.337: W/System.err(5838): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-17 13:45:29.337: W/System.err(5838): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-17 13:45:29.337: W/System.err(5838): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-17 13:45:29.337: W/System.err(5838): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-17 13:45:29.337: W/System.err(5838): at com.xcommerce.androidstore.Profile.postJson(Profile.java:123)
09-17 13:45:29.337: W/System.err(5838): at com.xcommerce.androidstore.Profile$1.onClick(Profile.java:70)
09-17 13:45:29.337: W/System.err(5838): at android.view.View.performClick(View.java:4202)
09-17 13:45:29.337: W/System.err(5838): at android.view.View$PerformClick.run(View.java:17340)
09-17 13:45:29.337: W/System.err(5838): at android.os.Handler.handleCallback(Handler.java:725)
09-17 13:45:29.337: W/System.err(5838): at android.os.Handler.dispatchMessage(Handler.java:92)
09-17 13:45:29.337: W/System.err(5838): at android.os.Looper.loop(Looper.java:137)
09-17 13:45:29.337: W/System.err(5838): at android.app.ActivityThread.main(ActivityThread.java:5039)
09-17 13:45:29.337: W/System.err(5838): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 13:45:29.337: W/System.err(5838): at java.lang.reflect.Method.invoke(Method.java:511)
09-17 13:45:29.337: W/System.err(5838): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-17 13:45:29.337: W/System.err(5838): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-17 13:45:29.337: W/System.err(5838): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
尝试以下
public String postJson(String url,JSONObject obj)
{
String string = "";
HttpGet confirmGet;
try
{
HttpClient hc = new DefaultHttpClient();
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
try
{
if(obj != null)
{
params.add(new BasicNameValuePair("", obj.toString()));
Log.d("request", " " + obj.toString());
}
}catch (Exception e)
{
e.printStackTrace();
}
try
{
new URLEncodedUtils();
confirmGet = new HttpGet(URI.create(url + "?" + URLEncodedUtils.format(params,"utf-8")));
HttpResponse confirmResponse = hc.execute(confirmGet);
string = EntityUtils.toString(confirmResponse.getEntity());
} catch (Exception e)
{
e.printStackTrace();
string = "Exception";
}
}catch (Exception e)
{
e.printStackTrace();
}
return string;
}
它会返回你的json字符串。 URL将是您要发送该json对象的URL。
一旦你有json回复,你需要打电话
jsonobject newJson = new JsonObject(string);