這裏我想顯示特定用戶的元素到android的列表視圖中,但在響應我得到了錯誤作爲「在json的字符0的輸入結束」以及nullpointerexception,所以任何人都可以向我建議解決方案!如何顯示從web到用戶的元素到android的列表視圖android
//MainActivity.Java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
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.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String REGISTER_URL = "http://xx.xx.xx.xx/api.php";
public static final String KEY_USERNAME = "id";
public static final String KEY_PASSWORD = "api_key";
public static final String KEY_EMAIL = "type";
ArrayList<HashMap<String, String>> contactList;
private EditText editTextUsername;
private EditText editTextEmail;
private EditText editTextPassword;
String result="";
private ListView lv;
private Button buttonRegister;
private Button buttonLogin;
private TextView txtview;
private String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.list);
contactList = new ArrayList<>();
// txtview=(TextView) findViewById(R.id.textView);
buttonLogin = (Button) findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(this);
}
private void UserShow(){
final String username = "117";
final String password = "pass";
final String email="get_earn";
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
InputStream ss=null;
//convert response to string
try{
//BufferedReader reader = new BufferedReader(new InputStreamReader(ss,"iso-8859-1"),8);
BufferedReader reader = new BufferedReader(new InputStreamReader(ss,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
ss.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try {
JSONObject jsonObj = new JSONObject(result);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("result");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("name");
String name = c.getString("disc");
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("name", id);
contact.put("disc", name);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList,
R.layout.list_item, new String[]{ "name","disc"},
new int[]{R.id.email, R.id.mobile});
lv.setAdapter(adapter);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("id","username");
params.put("api_key","pasword");
params.put("type", "email");
return params;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(50000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
public void onClick(View v) {
if(v == buttonRegister){
registerUser();
}
if(v == buttonLogin){
UserShow();
}
}
}
@CoDFather我想要使用post方法爲那 – Rkboss