1.导入okhttp的jar包
2.请求的方法
第一处写要发送的json数据,第二处写要请求的地址
该方法的完整代码:
private void sendRequestWithOkHttp() {
new Thread(new Runnable() {
@Override
public void run() {
JSONObject obj = new JSONObject();
try {
obj.put("UserName","user1");
obj.put("UserPwd","123457");
} catch (JSONException e) {
e.printStackTrace();
}
MediaType type = MediaType.parse("application/json;charset=utf-8");
RequestBody RequestBody2 = RequestBody.create(type,""+obj.toString());
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
// 指定访问的服务器地址
.url("http://172.20.10.7:8080/transportservice/action/user_login.do").post(RequestBody2)
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
parseJSONWithJSONObject(responseData);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
3.接收的方法
拿返回的key值接收即可
该方法的完整代码:
private void parseJSONWithJSONObject(String jsonData) {
try {
JSONObject object=new JSONObject(jsonData);
String name = object.getString("RESULT");
//日志
Log.d("name", "结果是:"+name);
} catch (JSONException e) {
e.printStackTrace();
}
Activity完整代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendRequest = (Button) findViewById(R.id.send_request);
sendRequest.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.send_request) {
//sendRequestWithHttpURLConnection();
sendRequestWithOkHttp();
}
}
private void sendRequestWithOkHttp() {
new Thread(new Runnable() {
@Override
public void run() {
JSONObject obj = new JSONObject();
try {
obj.put("UserName","user1");
obj.put("UserPwd","123457");
} catch (JSONException e) {
e.printStackTrace();
}
MediaType type = MediaType.parse("application/json;charset=utf-8");
RequestBody RequestBody2 = RequestBody.create(type,""+obj.toString());
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
// 指定访问的服务器地址
.url("http://172.20.10.7:8080/transportservice/action/user_login.do").post(RequestBody2)
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
parseJSONWithJSONObject(responseData);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
private void parseJSONWithJSONObject(String jsonData) {
try {
JSONObject object=new JSONObject(jsonData);
String name = object.getString("RESULT");
//日志
Log.d("name", "结果是:"+name);
} catch (JSONException e) {
e.printStackTrace();
}
}
}