Jia*_*mpo 8 php android json android-studio
我一直在尝试创建一个 json 格式以在 android 应用程序上显示它,以便我可以在我的电子钱包项目中使用它,但我一直收到错误访问拒绝找到属性“ro.serialno”或“org.json.JSONException:字符“输入结束
我认为连接的主要原因是访问被拒绝的错误,也是在运行调试之前我已经看到很多被拒绝的权限。
这是我登录电子钱包的代码:
package RAMS.e_wallet;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class Login extends AppCompatActivity {
private static EditText etNumber, etPassword;
private static String phone = null;
private static final String password = null;
private static final String URL = "http://192.168.1.218/login1/login/login.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etNumber = findViewById(R.id.username);
etPassword = findViewById(R.id.password);
}
public void backToMenu (View v){
phone = etNumber.getText().toString().trim();
if(!phone.equals("")){
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
Toast.makeText(Login.this, jsonObject.getString("success"),Toast.LENGTH_LONG).show();
}
catch (JSONException e){
e.printStackTrace();
Toast.makeText(Login.this, "e"+e.toString(), Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Login.this, "err"+error.toString(),Toast.LENGTH_LONG).show();
}
}){
@NotNull
@Override
public Map<String, String> getParams() throws AuthFailureError {
Map<String, String> data = new HashMap<>();
data.put("phone_number", phone);
return data;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
}
else {
Toast.makeText(this, "Fields cannot be empty!", Toast.LENGTH_SHORT).show();
}
}
public void switchRegister (View view){
Intent intent = new Intent(this, Register.class);
startActivity(intent);
finish();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我对 gradle 的依赖
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.3'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
testImplementation 'com.squareup.okhttp3:mockwebserver:4.9.0'
}
Run Code Online (Sandbox Code Playgroud)
我用于登录的 PHP 代码
<?php
class Reply{
function procced(){
if(isset($_POST['phone']))
{
require_once "conn.php";
require_once "validate.php";
$phone = var_dump($_POST['phone']);
$sql = "select * from users where phone_number= '$phone'";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
session_start();
$data=["response"=>"success"];
return json_encode($data);
}
else{
$data=["response"=>"error"];
return json_encode($data);
}
}
$data=["response"=>"error"];
return json_encode($data);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
和我一直在尝试的 Json 格式:
package com.example.json;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DownloadManager;
import android.os.Bundle;
import android.widget.TextView;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private TextView mTextview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextview = findViewById(R.id.textviewresult);
OkHttpClient client = new OkHttpClient();
String url = "http://192.168.1.218/jsonlogin/json.php";
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
if(response.isSuccessful()){
String myResponse = response.body().toString();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
mTextview.setText(myResponse);
}
});
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
json 代码处于不同的活动中,因为我正在尝试为 textview 显示大量数据
此外,所有代码在其清单上都有互联网许可
| 归档时间: |
|
| 查看次数: |
3228 次 |
| 最近记录: |