软工每日总结29

今天的主要工作就是将数据处理的客户端部分完成,从同学写好的安卓交互程序中读取用户名和密码等信息,并将其提交给我的服务器后台程序处理,并读取反馈信息反馈给用户交互模块,主要涉及到http连接以及json数据的处理等内容。

主要代码

private Handler handler=new Handler(){
public void handleMessage(Message msg){
switch (msg.arg1) {
case 1:
my_json mj;
mj=take_json(resultData);
Toast toast=Toast.makeText(getApplicationContext(), mj.id+" "+mj.state+" "+mj.code, Toast.LENGTH_SHORT);
toast.show();
//mTextView.setText(mj.id+" "+mj.state+" "+mj.code);
break;
default:
break;
}
}
};
class my_json
{
public int id;
public String state;
public String code;
}
private my_json take_json(String JSON)
{
my_json mj=new my_json();
try {
JSONTokener jsonParser = new JSONTokener(JSON);
// 此时还未读取任何json文本,直接读取就是一个JSONObject对象。
// 如果此时的读取位置在"name" : 了,那么nextValue就是"yuanzhifei89"(String)
JSONObject person = (JSONObject) jsonParser.nextValue();
// 接下来的就是JSON对象的操作了

mj.id=person.getInt("id");
mj.state=person.getString("state");
mj.code=person.getString("code");
} catch (JSONException ex) {
// 异常处理代码
}
return mj;
}
private class httpThread implements Runnable{

public void run() {
// TODO Auto-generated method stub
URL url = null;
try {
name=username;
pw=password;
String s="http://www.evi1.pw/map/user.php";//?key=elsKey&mod="+mod+"&name="+name+"&pw="+pw;
url = new URL(s);
Log.e("url", " " + s);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection urlConnection= null;
try {
assert url != null;
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);

StringBuilder params = new StringBuilder();
// 表单参数与get形式一样
params.append("mod").append("=").append(mod)
.append("&").append("email").append("=").append(name)
.append("&").append("pw").append("=").append(pw);
Log.e("params", " " + params.toString());
byte[] bypes = params.toString().getBytes();
urlConnection.getOutputStream().write(bypes);// 输入参数
} catch (IOException e) {
e.printStackTrace();
}
try {
assert urlConnection != null;
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
//readStream(in);
InputStreamReader isr = new InputStreamReader(in);
BufferedReader bufferReader = new BufferedReader(isr);
String inputLine = "";
resultData="";
while((inputLine = bufferReader.readLine()) != null){
resultData += inputLine ;
Log.e("return", " " + inputLine);
}
Message msgMessage=new Message();
msgMessage.arg1=1;
handler.sendMessage(msgMessage);

} catch (IOException e) {
e.printStackTrace();
} finally {
assert urlConnection != null;
urlConnection.disconnect();
}
}

}
posted @ 2016-06-29 20:41  13070026王志东  阅读(121)  评论(0编辑  收藏  举报