我正在尝试使用 Volley 并在登录后发送 GET 请求以检查数据。
我有专门的“API”班级,所有 Volley 员工都在那里。
我想要做的是在 LoginActivity(默认 android studio 模板)中检查验证后,我想在 LoginActivity 中创建 API 实例并将 GET 请求发送到服务器。
这仅用于测试 Volley,稍后我想在某个 Main 类中创建一个 API 实例。
这是我的 API 类:
import android.app.Application;
import android.util.Log;
import com.android.volley.*;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONObject;
import java.lang.String;
public class API extends Application {
protected String ServerURL;
protected String GET;
protected String POST;
protected RequestQueue queue;
public API(){
setServerURL("http://localhost:63424");
setGET("http://localhost:63424/api");
setPOST("http://localhost:63424/api");
queue = Volley.newRequestQueue(this);
}
protected void SendGET (String request)
{
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, …Run Code Online (Sandbox Code Playgroud) 大家好,有人可以帮我上这门课吗?我收到一个错误,我只是 android studio 和移动开发的新手。我正在学习和获取知识的过程中。请帮我。
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(Register.this, response, Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Register.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(NAME, pangaran);
params.put(SURNAME, apelyido);
params.put(ADDRESS, lugar);
params.put(CONTACTNO, kontak);
params.put(USERNAME, username1);
params.put(PASSWORD, password1);
return params;
}
};
requestQueue.add(stringRequest);
Run Code Online (Sandbox Code Playgroud) 所以我正在尝试登录我的应用程序,当我在插入用户名和密码后单击登录按钮时,此错误出现在 android studio 左下角底部工具栏的运行选项卡中:
E/Volley: [11996] BasicNetwork.performRequest: Unexpected response code 301 "MYURL"
Run Code Online (Sandbox Code Playgroud)
与我的网站链接,仅此而已。您可以在此处了解有关错误 HTTP 301 的更多信息!这是我的代码:
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class login extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.login, container, false);
final EditText etUtilizador = mView.findViewById(R.id.etUtilizador);
final EditText etPassword = mView.findViewById(R.id.etPassword);
final Button …Run Code Online (Sandbox Code Playgroud) 我有以下 json 响应
{"multicast_id":8XXXD,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:14XX"}]}
Run Code Online (Sandbox Code Playgroud)
我想检查 java 中是否failure = 0。以下是我的排球代码。
StringRequest stringRequest = new StringRequest(Request.Method.POST, CONN_URL.send_single_push,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getActivity(), response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("title", title);
params.put("message", message);
if (!TextUtils.isEmpty(image))
params.put("image", image);
params.put("course", course);
return params;
}
};
MySingleton.getInstance(getActivity()).addToRequestQueue(stringRequest);
Run Code Online (Sandbox Code Playgroud)
我是安卓新手。请帮我解决这个问题。
我在创建意图以及使用 getParcelableExtra() 方法将对象移交到它时遇到问题。该项目的目标是创建回收者视图。如果选择了回收器中的某个项目,则会启动新意图并应显示更详细的数据。
因为数据是从外部 MySQL DB 获取的,所以我使用 Volley 来处理大部分网络内容。
回收器在 Volley onResponse() 方法内实现,该方法首先在应用程序启动 (onCreate) 时调用。到目前为止,一切正常,回收器已正确加载和显示。
public class UserAreaActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_area);
initializeRecycler();
}
public void initializeRecycler() {
operations.getFoodFromDB(getFoodID(), new IDatabaseOperations() {
@Override
public void onSuccess(final ArrayList<Food> food_list) {
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mLayoutmanager = new LinearLayoutManager(UserAreaActivity.this);
mAdapter = new FoodAdapter(food_list);
mRecyclerView.setLayoutManager(mLayoutmanager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new FoodAdapter.OnItemClickListener() {
@Override
public void OnItemClick(int position) {
Intent intent = new Intent(UserAreaActivity.this, FoodProfile.class);
GETS CORRECT OBJECT----->intent.putExtra("food", food_list.get(position));
startActivity(intent);
} …Run Code Online (Sandbox Code Playgroud) 我想发送一个关于 volley 的 HTTP 帖子。根据IDE,我不能覆盖方法getBodyContentType和getBody,网络上的一些示例是否显示了这一点。它的语法(在大括号中)是什么?我究竟做错了什么?
错误消息:修饰符“覆盖”不适用于“本地函数”
val textView = findViewById<TextView>(R.id.loginStatus)
val queue = Volley.newRequestQueue(this)
val url = "http://example.com/v1/user/read"
val stringRequest = StringRequest( Request.Method.GET, url,
Response.Listener<String> { response ->
textView.text = "Response is here"
},
Response.ErrorListener { textView.text = "Error." }
){
override fun getBodyContentType(): String {
return "application/json"
}
override fun getBody(): ByteArray {
return "{\"email\":\"123@foo.de\", \"passwort\":\"123\"}".toString().toByteArray()
}
}
Run Code Online (Sandbox Code Playgroud) 齐射请求很快就完成了。它无缘无故地重复(看似)例如,当我想提出两个请求时,volley 会执行 4 次。下面是我的代码,你们中的一些人可以暗示我的代码中的问题吗?
while(i<chnumTxt.length()){
final RequestQueue queue;
queue = Volley.newRequestQueue(this);
char letter=chnumTxt.charAt(i);
Log.i("check","counter="+i+" "+"digit="+letter);
String URL = "http://192.168.4.20:80/chnumber?key="+letter;
Log.i("web",URL);
StringRequest request = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Log.i("html",response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(request);
new CountDownTimer(500, 500) {
public void onFinish() {
// When timer is finished
// Execute your code here
}
public void onTick(long millisUntilFinished) {
}
}.start();
i++;
}
Run Code Online (Sandbox Code Playgroud) 我到处都读到凌空将使用HttpUrlConnection用于较新版本的api或HttpClient用于旧版本,我试图告诉凌空只使用HttpUrlConnection.
我的主要目标是使用我存储的cookie设置volley来执行请求,为此我知道我需要使用cookie设置HttpUrlConnection,然后将其传递给volley以用作默认实现.
到目前为止这么好,但我不知道如何启动HttpUrlConnection并添加cookie.
有人可以给我一个小例子,说明如何初始化HttpUrlConnection并为其添加一个cookie,然后将其传递给凌空?
我能够在HttpUrlConnection上执行这样的请求并且它有效,但是如何将其设置为与凌空一起使用?
URL urlLink = new URL(url2);
HttpURLConnection conenction = (HttpURLConnection)urlLink.openConnection();
conenction.setRequestProperty("Cookie", cookie);
Run Code Online (Sandbox Code Playgroud) 我得到这个错误
Error:(42, 38) error: cannot find symbol class newRequestQueue
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.
Run Code Online (Sandbox Code Playgroud)
在线:
RequestQueue queue=new Volley.newRequestQueue(this);
Run Code Online (Sandbox Code Playgroud) 我需要手动设置排球的标题,但我必须发送字符串和Int.
我知道我应该这样做:
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> mHeaders = new HashMap<>();
mHeaders.put("Name", "James");
mHeaders.put("Country", "UK");
return mHeaders;
}
Run Code Online (Sandbox Code Playgroud)
但我需要发送一些Int类型的值.
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> mHeaders = new HashMap<>();
mHeaders.put("Name", "James");
mHeaders.put("Country", "UK");
mHeaders.put("Age", 21);
return mHeaders;
}
Run Code Online (Sandbox Code Playgroud)
但它应该发送一个字符串,但WebService需要一个Int.