gistfile1.txt public void okHttp(){ new Thread(){ @Override public void run() { /* try { client=new OkHttpClient(); request = new Request.Builder() .url("http://192.168.2.5:8080/transportservice/type/jason/action/GetCarAccountBalance.do") .
public void okHttp(){
new Thread(){
@Override
public void run() {
/* try {
client=new OkHttpClient();
request = new Request.Builder()
.url("http://192.168.2.5:8080/transportservice/type/jason/action/GetCarAccountBalance.do")
.build();
client.newCall(request).execute().;
String responseData= response.body().string();
parsejson(responseData);
} catch (Exception e) {
e.printStackTrace();
}*/
//传的参数
String jsonCar = "";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("CarId",4);
jsonCar = jsonObject.toString();
} catch (JSONException e) {
e.printStackTrace();
}
String url = "http://192.168.2.5:8080/transportservice/type/jason/action/GetCarAccountBalance.do";
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf8"),jsonCar); //RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonCar);
Request request = new Request.Builder().url(url).post(requestBody).build();
client=new OkHttpClient();
PayFragment.java
package mad.com.its02.fragment;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import mad.com.its02.MainActivity;
import mad.com.its02.R;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import static okhttp3.RequestBody.*;
/**
* A simple {@link Fragment} subclass.
*/
public class PayFragment extends Fragment {
private Button chongzhi;
private Button check;
private SimpleDateFormat sm;
private EditText et_chongzhi;
private TextView balance;
private AlertDialog.Builder dialog=null;
private OkHttpClient client;
private Request request;
private Response response;
private JSONObject jsonObject;
private JSONArray jsonArray;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
String data= (String) msg.obj;
int i = Integer.parseInt(data);
balance.setText(i);
super.handleMessage(msg);
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_pay, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
chongzhi= (Button) getActivity().findViewById(R.id.chongzhi);
balance= (TextView) getActivity().findViewById(R.id.balance);
check= (Button) getActivity().findViewById(R.id.check);
sm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
et_chongzhi= (EditText) getActivity().findViewById(R.id.et_chongzhi);
chongzhi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog=new AlertDialog.Builder(getContext());
dialog.setTitle("账户充值");
dialog.setMessage("充值金额为:"+et_chongzhi.getText().toString()+"元,现在是北京时间"+sm.format(new Date()));
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getContext(),"充值成功",Toast.LENGTH_SHORT).show();
}
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
dialog=new AlertDialog.Builder(getContext());
dialog.setTitle("提示");
dialog.setMessage("请问你真的要取消充值吗,可能会影响你的正常使用?");
dialog.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});dialog.show();
}
});
dialog.show();
}
});
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId()==R.id.check){
okHttp();
}
}
});
}
//从服务端获取json数据
public void okHttp(){
new Thread(){
@Override
public void run() {
/* try {
client=new OkHttpClient();
request = new Request.Builder()
.url("http://192.168.2.5:8080/transportservice/type/jason/action/GetCarAccountBalance.do")
.build();
client.newCall(request).execute().;
String responseData= response.body().string();
parsejson(responseData);
} catch (Exception e) {
e.printStackTrace();
}*/
//传的参数
String jsonCar = "";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("CarId",4);
jsonCar = jsonObject.toString();
} catch (JSONException e) {
e.printStackTrace();
}
String url = "http://192.168.2.5:8080/transportservice/type/jason/action/GetCarAccountBalance.do";
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf8"),jsonCar); //RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonCar);
Request request = new Request.Builder().url(url).post(requestBody).build();
client=new OkHttpClient();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("联网失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String s = response.body().string();
try {
JSONObject object = new JSONObject(s);
String serverinfo = object.getString("serverinfo");
JSONObject jsonObject1 = new JSONObject(serverinfo);
int balance = jsonObject1.getInt("Balance");
Message msg=new Message();
msg.obj=balance;
handler.sendMessage(msg);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}.start();
}
//解析jsonshuju
/* private void parsejson(String jsonData ) {
try {
JSONObject jsonObject = new JSONObject(jsonData);
String serverinfo = jsonObject.getString("serverinfo");
JSONObject jsonObject1 = new JSONObject(serverinfo);
Object o = jsonObject.get(String.valueOf(jsonObject1));
Message msg=new Message();
msg.obj=o;
} catch (JSONException e1) {
e1.printStackTrace();
}
}*/
}
fragment_pay.xml
