Android 访问 Webapi 更新UI

首先,写一个访问webapi的工具类

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject; import java.io.IOException;
import java.util.List; /**
* Created by Tyler on 2018/1/18
*/ public class HttpWebapi {
public static String DoGet(String url) throws Exception { HttpGet httpGet = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse resp = client.execute(httpGet);
HttpEntity he = resp.getEntity();
String respContent = EntityUtils.toString(he, "UTF-8"); return respContent; } public static String DoPost(String url, List<NameValuePair> nvps) throws Exception {
HttpPost httpost = new HttpPost(url);
HttpClient client = new DefaultHttpClient();
httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
HttpResponse resp = client.execute(httpost);
HttpEntity he = resp.getEntity();
String respContent = EntityUtils.toString(he, "UTF-8");
return respContent;
} public static String DoPost(String url, JSONObject template)throws Exception{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
httpPost.setEntity(new StringEntity(template.toString(),"utf-8"));
HttpResponse resp = httpClient.execute(httpPost);
String respContent = EntityUtils.toString(resp.getEntity(), "UTF-8");
return respContent;
} //测试
public static String getRequest(String url) {
HttpClient hc = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
// HttpEntity entity = rp.getEntity();
HttpResponse rp;
try {
rp = hc.execute(get);
int statusCode = rp.getStatusLine().getStatusCode();
return statusCode + "";
} catch (ClientProtocolException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return "";
} }
 

使用的jar:

Android 访问 Webapi 更新UI

具体调用方式:http://blog.csdn.net/hanjun0612/article/details/73741127

然后主界面

public class MainActivity extends AppCompatActivity {
EditText editText;
//Handler更新UI
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg){
//信息接收完成
if(msg.what==1){
editText.setText(msg.toString());
}
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText);
//得到按钮实例
Button hellobtn = (Button) findViewById(R.id.hellobutton);
//设置监听按钮点击事件
hellobtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//开启线程,抓取数据
new Thread(new Runnable() {
@Override
public void run() {
try { String json = HttpWebapi.DoGet("http://192.168.1.88:802/api/WMS_Product/Test");
Message msg = Message.obtain();
msg.obj = json;
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
} }
}).start(); }
}); } }

UI界面:

<Button
android:id="@+id/hellobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="320dp"
android:text="@string/button_send"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <EditText
android:id="@+id/editText"
android:layout_width="375dp"
android:layout_height="299dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="4dp"
android:ems="10"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
上一篇:Android Studio 动态调试 apk 反编译出的 smali 代码


下一篇:Azure CosmosDB (14) 使用Postman访问CosmosDB REST API