how should i post this one in next activity by clicking the button Retrieve
但我不知道如何使用在线数据库000webhost对此进行编码。我的问题是当我点击按钮检索它将在下一个活动中发布数据库的内容,这是来自MainActivity.class的print.class这是我的代码到目前为止
MainActivity.class
package com.example.child.retrieve;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.NetworkResponse;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.Volley;
import com.example.child.retrieve.LoginRequest;
import com.example.child.retrieve.R;
import com.example.child.retrieve.print;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button bLogin = (Button)findViewById(R.id.ret);
bLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if(success) {
Intent intent = new Intent(MainActivity.this, print.class);
MainActivity.this.startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(responseListener);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
queue.add(loginRequest);
}
});
}
/* import com.android.volley.toolbox.HttpHeaderParser; */
public void onErrorResponse(VolleyError error) {
// As of f605da3 the following should work
NetworkResponse response = error.networkResponse;
if (error instanceof ServerError && response != null) {
try {
String res = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, "utf-8"));
// Now you can use any deserializer to make sense of data
JSONObject obj = new JSONObject(res);
} catch (UnsupportedEncodingException e1) {
// Couldn't properly decode data to string
e1.printStackTrace();
} catch (JSONException e2) {
// returned data is not JSONObject?
e2.printStackTrace();
}
}
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.child.retrieve.MainActivity">
<Button
android:id="@+id/ret"
android:text="Retrieve"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
正在加载正在加载数据库的LoginRequest.class
package com.example.child.retrieve;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Child on 1/31/2017.
*/
public class LoginRequest extends StringRequest {
private static final String LOGIN_REQUEST_URL = "http://cedportfolio.000webhostapp.com/Login.php";
private Map<String, String> params;
public LoginRequest(Response.Listener<String> listener) {
super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
params = new HashMap<>();
}
@Override
public Map<String, String> getParams() {
return params;
}
}
print.class
package com.example.child.retrieve;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by Child on 7/21/2017.
*/
public class print extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.print);
}
}
答案 0 :(得分:1)
如果要将数据从一个活动传递到另一个活动,则需要创建一个Parcelable
对象。查看this answer
你会发现这样的事情:
public class MyData implements Parcelable {
...
}
解析响应并将其传递给新活动
MyData data = parseJson(jsonResponse)
Intent intent = new Intent(MainActivity.this, print.class);
intent.putParcelable("data", data);
MainActivity.this.startActivity(intent);
在新活动中恢复
public class print extends Activity {
...
MyData data = getIntent().getParcelableExtra("data");
...
}
请注意,在活动之间传递大型对象可能会导致性能下降。您应该考虑其他策略,例如传递一些ID并在&#34;接收器&#34;处获取数据。活性