我有布局xml名称main.xml,我在该类中有main.java类,现在我发送网络调用并从休息服务中获取数据。但是现在我想在anohther类中分离网络调用。我怎么能这样做。
main.xml中
<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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/callsubclass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Call sub-class" />
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
main.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonCallSubClass = (Button)findViewById(R.id.callsubclass);
textResult = (TextView)findViewById(R.id.result);
// i can set network call here and update TExtview
textResult .setText(result);
}
休息服务回复= {"id":1958,"content":"Hello, World!"}
我单独的nework.java类
String url1 = String.format("http://rest-service.guides.spring.io/greeting");
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url1, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("Test", response.toString());
String token="a";
String id="";
String result="";
try {
id= response.get("id").toString();
result= response.get("content").toString();
}catch (JSONException e){
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
现在我想在单独的类中进行网络调用给出解决方案。