我正在使用排球库创建一个简单的数据库。我在选择要将其发送到哪个特定数据库时遇到问题。在我解释我的具体问题之前是代码:
package com.example.joker.sqltrial;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
/* *
* Created by Joker on 2016-12-13.
*/
public class PostRequest extends StringRequest {
private static String REGISTER_REQUEST_URL="";
private Map<String, String> params;
public PostRequest(String animal, int sizeClass, String spType, String description, Response.Listener<String> listener){
super (Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("animal", animal);
params.put("size class", sizeClass+"");
params.put("specie type", spType);
params.put("description", description);
}
@Override
public Map<String, String> getParams() {
return params;
}
public static void urlSet(String URL){
REGISTER_REQUEST_URL=URL;
}
}
所以在这行代码“super(Method.POST,REGISTER_REQUEST_URL,listener,null);”,我提供了一个应该是最终字符串的URL ...我想改变那个URL(表/它将去哪里,取决于用户输入的内容。在另一个类中,我选择了URL,这取决于从微调器中选择的内容,然后传递该url,抛出另一个使用对象将其传递给此类的方法。这是其他类的代码
btButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String animal = etAnimal.getText().toString();
final String sizeClass = etsizeClass.getText().toString();
final int spType = Integer.parseInt(etspType.getText().toString());
final int blaha = Integer.parseInt(etBlah.getText().toString());
final String blahBlah = etBlahBlah.getText().toString();
switch (category) {
case "human":
tempURL ="SOME SPECIFIC URL";
setURL(tempURL);
break;
case "alien":
tempURL ="SOME SPECIFIC URL";
setURL(tempURL);
break;
case "animal":
tempURL ="SOME SPECIFIC URL";
setURL(tempURL);
break;
default:
tempURL=null;
Toast.makeText(getBaseContext(), "try again and input correct characters", Toast.LENGTH_LONG).show();
}
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 i = new Intent(MainActivity.this, ViewAd.class);
MainActivity.this.startActivity(i);
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Posting failed!")
.setNegativeButton("retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
PostRequest postRequest = new PostRequest(animal, sizeClass, spType, description, responseListener);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
queue.add(postRequest);
}
});
}
public void setURL(String URL){
tempURL=URL;
PostRequest.urlSet(URL);
}
...基本上当单击按钮时,响应侦听器将看到从微调器中拾取的内容,然后根据特定URL上的传递,该URL将在其他类中设置URL,甚至在调用之前...我究竟做错了什么?我知道它是一个简单的解决方案我无法弄明白。请有人帮帮我吗?谢谢。
这是我得到的堆栈跟踪:
01-19 21:06:20.300 221-542/? W/genymotion_audio: out_write() limiting sleep time 46802 to 39909
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at org.json.JSONObject.<init>(JSONObject.java:159)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at org.json.JSONObject.<init>(JSONObject.java:172)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at com.example.joker.sqltrial.MainActivity$1$1.onResponse(MainActivity.java:74)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at com.example.joker.sqltrial.MainActivity$1$1.onResponse(MainActivity.java:70)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at android.os.Looper.loop(Looper.java:136)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5001)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-19 21:06:20.596 1514-1514/com.example.joker.sqltrial W/System.err: at dalvik.system.NativeStart.main(Native Method)
01-19 21:06:22.620 221-542/? W/genymotion_audio: out_write() limiting sleep time 43809 to 39909
答案 0 :(得分:1)
public static void urlSet(String URL){
REGISTER_REQUEST_URL=URL;
}
通过使此方法和REGISTER_REQUEST_URL
静态,您说每个 PostRequest
对象都具有相同的网址。由于您要更改每个请求的URL,因此此方法和变量都不应该是静态的。相反,它们应该是非静态成员字段和方法。
此更改会导致您的代码出现其他一些问题。您需要弄清楚如何在正确的PostRequest
实例上设置URL。