Asynchttpclient的使用以及JSON数据传递
首先在dependencies 中添加依赖库
dependencies {
compile 'com.loopj.android:android-async-http:1.4.9'
}
在manifest中给网络权限
<uses-permission android:name="android.permission.INTERNET" />
新建类用于不用重复用网页访问
package com.example.abc.asynchttp;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;
/**
* Created by Administrator on 2018/4/10.
*/
public class UtilHttp {
private static final String API = "http://192.168.1.105:8890/type/jason/action/";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void post(android.content.Context context, java.lang.String url, cz.msebera.android.httpclient.HttpEntity entity, java.lang.String contentType, ResponseHandlerInterface responseHandler) {
client.post(context, getAbsoluteUrl(url), entity, contentType, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return API + relativeUrl;
}
}
MainActivity中传入路径
package com.example.abc.asynchttp;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.entity.ByteArrayEntity;
import cz.msebera.android.httpclient.message.BasicHeader;
import cz.msebera.android.httpclient.protocol.HTTP;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private Button btn1;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.main_web_btn);
btn1 = findViewById(R.id.main_btn1);
button.setOnClickListener(this);
btn1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_web_btn:
RequestParams params = new RequestParams();
params.put("username", "admin");
UtilHttp.get("getSensor", params, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Toast.makeText(MainActivity.this, "fail", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Toast.makeText(MainActivity.this, responseString, Toast.LENGTH_SHORT).show();
Log.e("123", "onSuccess: ............................" + responseString);
}
});
break;
case R.id.main_btn1:
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("Blower", 1);
} catch (JSONException e) {
e.printStackTrace();
}
ByteArrayEntity entity = null;
try {
entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
UtilHttp.post(mContext, "control", entity, "application/json", new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
Log.e("123456789", "11111111111111111654645646546411111111111111111");
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
Log.e("123456789", "2222222222222222222222222222222222222222222221313");
}
});
break;
}
}
}