用volley在Genymotion上获取网页源码

时间:2021-09-23 22:15:17

用volley在Genymotion上获取网页源码

 

XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools
="http://schemas.android.com/tools"
android:layout_width
="match_parent"
android:layout_height
="match_parent"
android:paddingBottom
="@dimen/activity_vertical_margin"
android:paddingLeft
="@dimen/activity_horizontal_margin"
android:paddingRight
="@dimen/activity_horizontal_margin"
android:paddingTop
="@dimen/activity_vertical_margin"
tools:context
="com.example.dell.fuwuqicunchu.fuwuqicunchu"
android:orientation
="vertical">

<EditText
android:layout_width
="match_parent"
android:layout_height
="wrap_content"
android:text
="Hello World!"
android:id
="@+id/et_1"/>
<LinearLayout
android:layout_width
="match_parent"
android:layout_height
="wrap_content">
<Button
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="volleyget"
android:onClick
="volleyget"
android:layout_weight
="1"/>

<Button
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="volleypost"
android:onClick
="volleypost"
android:layout_weight
="1"/>
</LinearLayout>

<EditText
android:layout_width
="match_parent"
android:layout_height
="wrap_content"
android:id
="@+id/et_2"/>
</LinearLayout>

JAVA代码

package com.example.dell.myfuwuqi;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

public class webActivity extends AppCompatActivity {

EditText et_1;
EditText et_2;

ProgressDialog progressDialog;

private android.os.Handler handler = new android.os.Handler(){

@Override
public void handleMessage(Message msg) {

String content
= (String)msg.obj;

et_2.setText(content);

//progressDialog.dismiss();


}
};

RequestQueue requestQueue;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);

et_1
=(EditText)findViewById(R.id.et_1);
et_1.setText(
"http://192.168.1.101:8080/Testweb/testweb.jsp");
et_2
=(EditText)findViewById(R.id.et_2);

requestQueue
=Volley.newRequestQueue(this);

}

public void volleyget(View view)
{
new Thread(){
@Override
public void run() {

// progressDialog = new ProgressDialog(webActivity.this);

StringRequest str
= new StringRequest(et_1.getText().toString(), new Response.Listener<String>() {
@Override
public void onResponse(String s) {

Message msg
= new Message();
msg.obj
=s;
handler.sendMessage(msg);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(webActivity.
this, "失败", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
});

requestQueue.add(str);


}
}.start();


}



public void volleypost(View v)
{
new Thread(){
@Override
public void run() {
// final ProgressDialog dialog = new ProgressDialog(webActivity.this);

StringRequest str
= new StringRequest(Request.Method.POST, et_1.getText().toString(), new Response.Listener<String>() {
@Override
public void onResponse(String s) {

Message msg
= new Message();
msg.obj
=s;
handler.sendMessage(msg);

}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {

Toast.makeText(webActivity.
this, "失败", Toast.LENGTH_SHORT).show();
//dialog.dismiss();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map
<String,String> map = new HashMap<String,String>();

map.put(
"name","volley");
return map;
}
};

requestQueue.add(str);

super.run();
}
}.start();

}
}

备注:

1、在4.0之后谷歌强制要求连接网络不能在主线程进行访问。

2、只有主线程(UI线程)才可以更显UI。