2017-04-10 66 views
0

这是我的代码,我正在使用json和凌云我想在下一个活动中显示响应,请详细告诉我您的答案,因为我是新的android我有这个错误org.json.JSONObject无法转换为JSONArray

public class MainActivity extends AppCompatActivity { 
Button btn_logn; 
EditText et_1, et_2; 
String url; 


String username, password; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    et_1 = (EditText) findViewById(R.id.email_one); 
    et_2 = (EditText) findViewById(R.id.pass_two); 


    btn_logn = (Button) findViewById(R.id.btn_login); 
    btn_logn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      login(); 
     } 
    }); 
} 

public void login() { 
    username = et_1.getText().toString(); 
    password = et_2.getText().toString(); 
    url = "http://rahatmedia.com/ivy_distribution_test/getEmployeeData.php?" + 
      "email=" + username + 
      "&password=" + password; 
    if (et_1.equals("") || et_2.equals("")) { 
     Toast.makeText(MainActivity.this, "some thing went wrong", Toast.LENGTH_SHORT).show(); 
    } else { 
     final StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       try { 

        JSONArray jsonArray = new JSONArray(response); 
        JSONObject jsonObject = jsonArray.getJSONObject(0); 
        String code = jsonObject.getString("code"); 


        if (code.equals("login_fail")) { 
         Toast.makeText(MainActivity.this, "error in login", Toast.LENGTH_SHORT).show(); 
        } else { 
         Intent i = new Intent(MainActivity.this, display.class); 
         Bundle bundle = new Bundle(); 
         bundle.putString("name", jsonObject.getString("name")); 
         bundle.putString("email", jsonObject.getString("email")); 
         bundle.putString("license_id ", jsonObject.getString("license_id")); 
         bundle.putString("current_loc ", jsonObject.getString("current_loc")); 
         bundle.putString("status ", jsonObject.getString("status")); 
         bundle.putString("password ", jsonObject.getString("password")); 
         bundle.putString("passcode ", jsonObject.getString("passcode")); 
         bundle.putString("mobile ", jsonObject.getString("mobile")); 
         bundle.putString("idcard ", jsonObject.getString("idcard")); 
         bundle.putString("address ", jsonObject.getString("address")); 
         bundle.putString("emptype ", jsonObject.getString("emptype")); 
         bundle.putString("gst_reg ", jsonObject.getString("gst_reg")); 
         bundle.putString("job_status ", jsonObject.getString("job_status")); 
         bundle.putString("joiningdate ", jsonObject.getString("joiningdate")); 
         bundle.putString("timestamp ", jsonObject.getString("timestamp ")); 
         i.putExtras(bundle); 
         startActivity(i); 
         Log.e("response", response); 
        } 
       } catch (JSONException e) { 

        Toast.makeText(MainActivity.this, "error in jasonexception", Toast.LENGTH_SHORT).show(); 
        Log.e("JSONException response", String.valueOf(e)); 
        e.printStackTrace(); 
       } 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError volleyError) { 
       Toast.makeText(MainActivity.this, "error in ErrorListener", Toast.LENGTH_SHORT).show(); 
      } 
     }) { 
      @Override 
      protected Map<String, String> getParams() throws AuthFailureError { 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("username", username); 
       params.put("password", password); 
       return params; 
      } 
     }; 
     singalton.getinstance(MainActivity.this).addtorequestque(stringRequest); 
    } 
} 

这是我的单身类

public class singalton { 
private static singalton minstance; 
private RequestQueue requestQueue; 
private Context context; 

private singalton(Context cont) { 
    context = cont; 
    requestQueue = getRequestQueue(); 



public RequestQueue getRequestQueue() { 
    if (requestQueue == null) { 
     requestQueue = Volley.newRequestQueue(context.getApplicationContext()); 
    } 
    return requestQueue; 
} 

public static synchronized singalton getinstance(Context context) { 
    if (minstance == null) { 
     minstance = new singalton(context); 
    } 
    return minstance; 

} 

public <T> void addtorequestque(Request<T> request) { 
    requestQueue.add(request); 
} 

,这是我的显示类

public class display extends AppCompatActivity { 
TextView tv_one, tv_two, tv_three, tv_four, tv_five, tv_six, tv_seven, tv_eight, tv_nine, tv_ten, tv_eleven, tv_twelve, tv_thirdeen, 
     tv_fourteen, tv_fifteen; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_display); 


    tv_one = (TextView) findViewById(R.id.txt_1); 
    tv_two = (TextView) findViewById(R.id.txt_2); 
    tv_three = (TextView) findViewById(R.id.txt_3); 
    tv_four = (TextView) findViewById(R.id.txt_4); 
    tv_five = (TextView) findViewById(R.id.txt_5); 
    tv_six = (TextView) findViewById(R.id.txt_6); 
    tv_seven = (TextView) findViewById(R.id.txt_7); 
    tv_eight = (TextView) findViewById(R.id.txt_8); 
    tv_nine = (TextView) findViewById(R.id.txt_9); 
    tv_ten = (TextView) findViewById(R.id.txt_10); 
    tv_eleven = (TextView) findViewById(R.id.txt_11); 
    tv_twelve = (TextView) findViewById(R.id.txt_12); 
    tv_thirdeen = (TextView) findViewById(R.id.txt_13); 
    tv_fourteen = (TextView) findViewById(R.id.txt_14); 
    tv_fifteen = (TextView) findViewById(R.id.txt_15); 


    Bundle bundle = getIntent().getExtras(); 


    tv_one.setText("name" + bundle.getString("name")); 
    tv_two.setText("email" + bundle.getString("email")); 
    tv_three.setText("license_id " + bundle.getString("license_id")); 
    tv_four.setText("current_loc " + bundle.getString("current_loc")); 
    tv_five.setText("status " + bundle.getString("status")); 
    tv_six.setText("password " + bundle.getString("password")); 
    tv_seven.setText("passcode " + bundle.getString("passcode")); 
    tv_eight.setText("mobile " + bundle.getString("mobile")); 
    tv_nine.setText("idcard " + bundle.getString("idcard")); 
    tv_ten.setText("address " + bundle.getString("address")); 
    tv_eleven.setText("emptype " + bundle.getString("emptype")); 
    tv_twelve.setText("gst_reg " + bundle.getString("gst_reg")); 
    tv_thirdeen.setText("job_status " + bundle.getString("job_status")); 
    tv_fourteen.setText("joiningdate " + bundle.getString("joiningdate")); 
    tv_fifteen.setText("timestamp " + bundle.getString("timestamp ")); 

} 
+0

正试图地图的JSONObject成JSONArray。我认为这部分代码是错误的:'JSONArray jsonArray = new JSONArray(response);' –

+0

好的请告诉我我该怎么做? –

+0

将其更改为'JSONObject',请阅读以下内容:http://www.androidhive.info/2012/01/android-json-parsing-tutorial/,或许nextime改用替代方式(对初学者更友好):http:/ /www.vogella.com/tutorials/Retrofit/article.html –

回答

0

从这个

JSONArray jsonArray = new JSONArray(response); 
        JSONObject jsonObject = jsonArray.getJSONObject(0); 
        String code = jsonObject.getString("code"); 

更改您的二三线这个

JSONObject jsonObject = new JSONObject(response); 
String code = jsonObject.getString("data"); 
+0

noe它说这在logcat“没有名称值” –

+0

现在* ............... –

+0

在哪一行?代码kaunsa线他 –

0

在你的api中。 我试过你的api这样http://rahatmedia.com/ivy_distribution_test/[email protected]&password=123456

它回馈一个jsonObj。因此,在您登录方法你已经采取它作为JSONArray ..就拿像JSONObject js = new JSONObject(responce);

+0

没有值它现在给这个错误“没有代码的价值” –

+0

它现在到哪一行? –

+0

我logcat的正显示出这种 “04-10 18:36:13.302 13083-13083/com.example.ehtisham.login2 E/JSONException响应:org.json.JSONException:代码没有价值” –