Java 截击-无法解析构造函数“;JSONObject请求
我关注的是这个问题 现在我加了一个闪屏,开始了截击请求 教程中说在发出JSONObjectRequest时要使用的代码是Java 截击-无法解析构造函数“;JSONObject请求,java,android,android-volley,Java,Android,Android Volley,我关注的是这个问题 现在我加了一个闪屏,开始了截击请求 教程中说在发出JSONObjectRequest时要使用的代码是 JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url, null, new Response.Listener<JSONObject>() JsonObjectRequest-jsonObjReq=newjsonobjectrequest(Method.GET,url,null
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url, null, new Response.Listener<JSONObject>()
JsonObjectRequest-jsonObjReq=newjsonobjectrequest(Method.GET,url,null,new Response.Listener())
但当我输入时,它要求我导入截击库,然后将其更改为
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>()
JsonObjectRequest-jsonObjReq=newjsonobjectrequest(Request.Method.GET,url,null,new Response.Listener())
这样做会导致无法解析构造函数的错误
我在SplashActivity.java中的代码是:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class SplashActivity extends Activity {
private static final String TAG = SplashActivity.class.getSimpleName();
private static final String TAG_FEED = "feed", TAG_ENTRY = "entry",
TAG_GPHOTO_ID = "gphoto$id", TAG_T = "$t",
TAG_ALBUM_TITLE = "title";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
setContentView(R.layout.activity_splash);
// Picasa request to get list of albums
String url = AppConst.URL_PICASA_ALBUMS
.replace("_PICASA_USER_", AppController.getInstance()
.getPrefManager().getGoogleUserName());
Log.d(TAG, "Albums request url: " + url);
// Preparing volley's json object request
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url,
null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "Albums Response: " + response.toString());
List<Category> albums = new ArrayList<Category>();
try {
// Parsing the json response
JSONArray entry = response.getJSONObject(TAG_FEED)
.getJSONArray(TAG_ENTRY);
// loop through albums nodes and add them to album
// list
for (int i = 0; i < entry.length(); i++) {
JSONObject albumObj = (JSONObject) entry.get(i);
// album id
String albumId = albumObj.getJSONObject(
TAG_GPHOTO_ID).getString(TAG_T);
// album title
String albumTitle = albumObj.getJSONObject(
TAG_ALBUM_TITLE).getString(TAG_T);
Category album = new Category();
album.setId(albumId);
album.setTitle(albumTitle);
// add album to list
albums.add(album);
Log.d(TAG, "Album Id: " + albumId
+ ", Album Title: " + albumTitle);
}
// Store albums in shared pref
AppController.getInstance().getPrefManager()
.storeCategories(albums);
// String the main activity
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(intent);
// closing spalsh activity
finish();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
getString(R.string.msg_unknown_error),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Volley Error: " + error.getMessage());
// show error toast
Toast.makeText(getApplicationContext(),
getString(R.string.splash_error),
Toast.LENGTH_LONG).show();
// Unable to fetch albums
// check for existing Albums data in Shared Preferences
if (AppController.getInstance().getPrefManager()
.getCategories() != null && AppController.getInstance().getPrefManager()
.getCategories().size() > 0) {
// String the main activity
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(intent);
// closing spalsh activity
finish();
} else {
// Albums data not present in the shared preferences
// Launch settings activity, so that user can modify
// the settings
Intent i = new Intent(SplashActivity.this,
SettingsActivity.class);
// clear all the activities
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
}
}
});
// disable the cache for this request, so that it always fetches updated
// json
jsonObjReq.setShouldCache(false);
// Making the request
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
}
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Window;
导入android.widget.Toast;
导入com.android.volley.Request;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonObjectRequest;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.List;
公共课堂活动扩展了活动{
私有静态最终字符串标记=SplashActivity.class.getSimpleName();
私有静态最终字符串TAG\u FEED=“FEED”,TAG\u ENTRY=“ENTRY”,
TAG_GPHOTO_ID=“GPHOTO$ID”,TAG_T=“$T”,
TAG_ALBUM_TITLE=“TITLE”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE\u ACTION\u栏);
getActionBar().hide();
setContentView(R.layout.activity_splash);
//Picasa请求获取相册列表
字符串url=AppConst.url\u PICASA\u相册
.replace(“\u PICASA\u USER\”,AppController.getInstance()
.getPrefManager().getGoogleUserName());
Log.d(标记“相册请求url:+url”);
//准备volley的json对象请求
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Request.Method.GET,url,
null,新响应。侦听器(){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(标记“Albums Response:+Response.toString());
列表相册=新建ArrayList();
试一试{
//解析json响应
JSONArray entry=response.getJSONObject(标记馈送)
.getJSONArray(标记输入);
//循环浏览相册节点并将其添加到相册
//名单
对于(int i=0;i0){
//把主要活动串起来
意向意向=新意向(getApplicationContext(),
主要活动(课堂);
星触觉(意向);
//关闭spalsh活动
完成();
}否则{
//共享首选项中不存在相册数据
//启动“设置”活动,以便用户可以修改
//设置
意向i=新意向(SplashActivity.this,
设置活动类);
//清除所有活动
i、 setFlags(Intent.FLAG\u活动\u新任务
|意图。标记活动(清除任务);
星触觉(i);
}
}
});
//禁用此请求的缓存,以便它始终获取更新的数据
//json
jsonObjReq.setShouldCache(false);
//提出请求
AppController.getInstance().addToRequestQueue(JSONOBJEQ);
}
}
我不确定如何解决此问题?请求构造函数中的参数不正确,因此您面临问题。创建
JsonOb的正确代码
JsonObjectRequest request = new JsonObjectRequest(Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject arg0) {
// TODO Auto-generated method stub
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
}
});
JsonObjectRequest request = new JsonObjectRequest(JsonRequest.Method.POST, url, (String)null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response){
pDialog.hide();
Log.d("Reponse", response.toString());
Toast.makeText(getApplicationContext(), response.optString("name"), Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
volleyError.printStackTrace();
Log.d("Error = ", volleyError.toString());
pDialog.hide();
}
}){
@Override
protected Map<String, String> getParams(){
return params;
};
}
compile 'com.mcxiaoke.volley:library:1.0.0'