java用服务端怎么用json数据传输到安卓客户端
1个回答
2017-05-02 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
java用服务端怎么用json数据传输到安卓客户端
public class MainActivity extends Activity
{
private Button submit = null;
URL url = null;
String urlPath = "http://10.0.3.2:8080/XMLParse/AcceptJsonServlet";
EditText name = null;
EditText age = null;
EditText address = null;
Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
if (msg.what == 0x123)
{
Toast.makeText(MainActivity.this,
"发送成功", Toast.LENGTH_LONG)
.show();
}
else
{
Toast.makeText(MainActivity.this,
"发送失败", Toast.LENGTH_LONG)
.show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = ((EditText) findViewById(R.id.name));
age = ((EditText) findViewById(R.id.age));
address = ((EditText) findViewById(R.id.address));
submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new Thread()
{
public void run()
{
JSONObject js = new JSONObject();
JSONObject params = new JSONObject();
// JSONArray array = new JSONArray();
Person p = new Person(name.getText().toString(), age
.getText().toString(), address.getText()
.toString());
// 封装子对象
try
{
js.put("name", p.getName());
js.put("age", p.getAge());
js.put("address", p.getAddress());
// 封装Person数组
params.put("Person", js);
}
catch (JSONException e)
{
e.printStackTrace();
}
// 把Json数据转换成String类型,使用输出流向服务器写
final String content = String.valueOf(params);
try
{
url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5000);
conn.setDoOutput(true);// 设置允许输出
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/json; charset=UTF-8"); // 内容类型
OutputStream os = conn.getOutputStream();
os.write(content.getBytes());
os.close();
if (conn.getResponseCode() == 200)
{
handler.sendEmptyMessage(0x123);
}
else
{
handler.sendEmptyMessage(0x122);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}.start();
public class MainActivity extends Activity
{
private Button submit = null;
URL url = null;
String urlPath = "http://10.0.3.2:8080/XMLParse/AcceptJsonServlet";
EditText name = null;
EditText age = null;
EditText address = null;
Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
if (msg.what == 0x123)
{
Toast.makeText(MainActivity.this,
"发送成功", Toast.LENGTH_LONG)
.show();
}
else
{
Toast.makeText(MainActivity.this,
"发送失败", Toast.LENGTH_LONG)
.show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = ((EditText) findViewById(R.id.name));
age = ((EditText) findViewById(R.id.age));
address = ((EditText) findViewById(R.id.address));
submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new Thread()
{
public void run()
{
JSONObject js = new JSONObject();
JSONObject params = new JSONObject();
// JSONArray array = new JSONArray();
Person p = new Person(name.getText().toString(), age
.getText().toString(), address.getText()
.toString());
// 封装子对象
try
{
js.put("name", p.getName());
js.put("age", p.getAge());
js.put("address", p.getAddress());
// 封装Person数组
params.put("Person", js);
}
catch (JSONException e)
{
e.printStackTrace();
}
// 把Json数据转换成String类型,使用输出流向服务器写
final String content = String.valueOf(params);
try
{
url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5000);
conn.setDoOutput(true);// 设置允许输出
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/json; charset=UTF-8"); // 内容类型
OutputStream os = conn.getOutputStream();
os.write(content.getBytes());
os.close();
if (conn.getResponseCode() == 200)
{
handler.sendEmptyMessage(0x123);
}
else
{
handler.sendEmptyMessage(0x122);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}.start();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询