-1

我为我的移动应用程序编写了一个 API。它位于http://tymonradzik.pl/THUNDER_HUNTER/thapi.php并以 JSON 格式返回数据。我想写一个登录方法,但是当我的应用程序询问 API 时,我收到了一个错误,我不知道那个错误的原因是什么。

这是我的登录功能的一部分:

public static String logIn(String nick, String password)
    {
        Log.e("2", "1"); //added by me to debug
        JSONParser jParser = new JSONParser();
        Log.e("2", "2"); //added by me to debug
        Log.wtf("Error !", "http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=" + nick + "&password=" + password + "&imei=");
        String json = jParser.getJSONFromUrl("http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=" + nick + "&password=" + haslo + "&imei=");
    Log.e("2", "3");
    Log.e("aaa",json.toString());
    JSONObject jObject;
    try {
        jObject = new JSONObject(json); //That is crash line !
        String error = jObject.getString("error");
        Log.e("ADSFDSFSDF", error);
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    Log.e("2", "3");

我的 JSONParser 类:

package com.radzik.thunter;


import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;

import org.json.JSONObject;



public class JSONParser {

   static InputStream is = null;
   static JSONObject jObj = null;
   static String json = "";

   // constructor
   public JSONParser() {

   }

   public String getJSONFromUrl(String url) {

       DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
       HttpPost httppost = new HttpPost(url);
       // Depends on your web service
       httppost.setHeader("Content-type", "application/json");

       InputStream inputStream = null;
       String result = null;
       try {
           HttpResponse response = httpclient.execute(httppost);           
           HttpEntity entity = response.getEntity();

           inputStream = entity.getContent();
           // json is UTF-8 by default
           BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
           StringBuilder sb = new StringBuilder();

           String line = null;
           while ((line = reader.readLine()) != null)
           {
               sb.append(line + "\n");
           }
           result = sb.toString();
       } catch (Exception e) { 
           // Oops
       }
       finally {
           try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
       }

       return result;
   }
}

我的日志猫:

07-24 13:11:08.440: E/2(18212): 1
07-24 13:11:08.440: E/2(18212): 2
07-24 13:11:08.440: A/Error !(18212): http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=aaa&password=bbb&imei=
07-24 13:11:08.475: D/AndroidRuntime(18212): Shutting down VM
07-24 13:11:08.480: W/dalvikvm(18212): threadid=1: thread exiting with uncaught exception (group=0x414e92a0)
07-24 13:11:08.485: E/AndroidRuntime(18212): FATAL EXCEPTION: main
07-24 13:11:08.485: E/AndroidRuntime(18212): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.radzik.thunter/com.radzik.thunter.ThunderHunter}: java.lang.NullPointerException
07-24 13:11:08.485: E/AndroidRuntime(18212):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at android.os.Looper.loop(Looper.java:137)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at android.app.ActivityThread.main(ActivityThread.java:4898)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at java.lang.reflect.Method.invokeNative(Native Method)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at java.lang.reflect.Method.invoke(Method.java:511)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at dalvik.system.NativeStart.main(Native Method)
07-24 13:11:08.485: E/AndroidRuntime(18212): Caused by: java.lang.NullPointerException
07-24 13:11:08.485: E/AndroidRuntime(18212):    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at org.json.JSONTokener.nextValue(JSONTokener.java:94)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at org.json.JSONObject.<init>(JSONObject.java:154)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at org.json.JSONObject.<init>(JSONObject.java:171)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at com.radzik.thunter.FunkcjeAPI.zalogujSie(FunkcjeAPI.java:20)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at com.radzik.thunter.ThunderHunter.onCreate(ThunderHunter.java:30)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at android.app.Activity.performCreate(Activity.java:5206)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
07-24 13:11:08.485: E/AndroidRuntime(18212):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
07-24 13:11:08.485: E/AndroidRuntime(18212):    ... 11 more

我不知道我失败的原因是什么。INTERNET我在我的 AndroidManifest.xml 中添加了一个权限。我logIn在我的主要活动中调用函数是这样的:

APIFunctions.logIn("aaa", "bbb"); 

NullPointerException 使用 James Holderness 回答:

07-26 16:42:41.665: E/AndroidRuntime(345): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.radzik.thunter/com.radzik.thunter.ThunderHunter}: java.lang.NullPointerException: println needs a message
07-26 16:42:41.665: E/AndroidRuntime(345):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-26 16:42:41.665: E/AndroidRuntime(345):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-26 16:42:41.665: E/AndroidRuntime(345):  at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-26 16:42:41.665: E/AndroidRuntime(345):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-26 16:42:41.665: E/AndroidRuntime(345):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 16:42:41.665: E/AndroidRuntime(345):  at android.os.Looper.loop(Looper.java:137)
07-26 16:42:41.665: E/AndroidRuntime(345):  at android.app.ActivityThread.main(ActivityThread.java:4898)
07-26 16:42:41.665: E/AndroidRuntime(345):  at java.lang.reflect.Method.invokeNative(Native Method)
07-26 16:42:41.665: E/AndroidRuntime(345):  at java.lang.reflect.Method.invoke(Method.java:511)
07-26 16:42:41.665: E/AndroidRuntime(345):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-26 16:42:41.665: E/AndroidRuntime(345):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-26 16:42:41.665: E/AndroidRuntime(345):  at dalvik.system.NativeStart.main(Native Method)
07-26 16:42:41.665: E/AndroidRuntime(345): Caused by: java.lang.NullPointerException: println needs a message
07-26 16:42:41.665: E/AndroidRuntime(345):  at android.util.Log.println_native(Native Method)
07-26 16:42:41.665: E/AndroidRuntime(345):  at android.util.Log.e(Log.java:297)
07-26 16:42:41.665: E/AndroidRuntime(345):  at com.radzik.thunter.FunkcjeAPI.zalogujSie(FunkcjeAPI.java:19)
07-26 16:42:41.665: E/AndroidRuntime(345):  at com.radzik.thunter.ThunderHunter.onCreate(ThunderHunter.java:30)
07-26 16:42:41.665: E/AndroidRuntime(345):  at android.app.Activity.performCreate(Activity.java:5206)
07-26 16:42:41.665: E/AndroidRuntime(345):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
07-26 16:42:41.665: E/AndroidRuntime(345):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
07-26 16:42:41.665: E/AndroidRuntime(345):  ... 11 more 
4

2 回答 2

2

客户:

import com.google.gson.Gson;

private static final String LOG = "Login layout";
private static final String URL = "xxx";

@SuppressWarnings("unused")
@Override
public void onClick(View view) {
    Resources standardResources = getApplicationContext().getResources();
    AssetManager assets = standardResources.getAssets();
    DisplayMetrics metrics = standardResources.getDisplayMetrics();
    Configuration config = new Configuration(standardResources.getConfiguration());
    Resources defaultResources;

    int id = view.getId();
    if (id == R.id.btnCheck) {
        if (isNetworkAvailable()) {
            try {
                if (!login.getText().toString().equals("")
                        && !password.getText().toString().equals("")) {
                    HttpComunication task = new HttpComunication();
                    task.login(login.getText().toString(), password
                            .getText().toString(), false);
                    task.execute(new String[] { URL });
                    if (!task.get().equals("0")) {
                        Log.i(LOG, task.get());
                        Profile profile = new Gson().fromJson(task.get(),
                                Profile.class);

                        Intent intent = new Intent(getApplicationContext(),
                                StopPointsActivity.class);
                        intent.putExtra("PROFILE", profile);
                        intent.putExtra("LOGIN", login.getText().toString());
                        intent.putExtra("PASS", password.getText()
                                .toString());

                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(
                                getApplicationContext(),
                                getResources().getString(
                                        R.string.incorect_data),
                                Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(getApplicationContext(),
                            getResources().getString(R.string.empty_data),
                            Toast.LENGTH_LONG).show();
                }

            } catch (Exception e) {
                Log.e(LOG, e.toString());
            }
        } else {
            Toast.makeText(getApplicationContext(),
                    getResources().getString(R.string.error_connection),
                    Toast.LENGTH_LONG).show();
        }
    }
}

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}



public class HttpComunication extends AsyncTask<String, Void, String> {
private String login;
private String password;
private boolean isLoged;
private String logReport;
private String start;
private String stop;

public void login(String _login, String _password, boolean _isLoged) {
    this.login = _login;
    this.password = _password;

    if (_isLoged) {
        logReport = "yes";
    } else {
        logReport = "no";
    }
    this.isLoged = _isLoged;
}

public void getPath(String _start, String _stop) {
    this.start = _start;
    this.stop = _stop;
}

@Override
protected String doInBackground(String... urls) {
    String output = null;
    if (!isLoged) {
        for (String url : urls) {
            output = loginToSystem(url);
        }
    } else {
        for (String url : urls) {
            output = getSchedules(url);
        }
    }
    return output;
}

private String loginToSystem(String url) {
    String output = null;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("username", login));
        postParameters.add(new BasicNameValuePair("password", password));
        postParameters.add(new BasicNameValuePair("loged", logReport));
        httpPost.setEntity(new UrlEncodedFormEntity(postParameters));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        output = EntityUtils.toString(httpEntity);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return output;
}

private String getSchedules(String url) {
    String output = null;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

        postParameters.add(new BasicNameValuePair("username", login));
        postParameters.add(new BasicNameValuePair("password", password));
        postParameters.add(new BasicNameValuePair("loged", logReport));
        postParameters.add(new BasicNameValuePair("start", start));
        postParameters.add(new BasicNameValuePair("stop", stop));
        httpPost.setEntity(new UrlEncodedFormEntity(postParameters));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        output = EntityUtils.toString(httpEntity);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return output;
}

@Override
protected void onPostExecute(String output) {
}

}

服务器:

protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    Profile result;
    String userName, password, start, stop;

    if (req.getParameter("loged").equals("no")) {

        userName = req.getParameter("username");
        password = req.getParameter("password");

        if (CheckData.comfirmData(userName, password)) {
            result = CheckData.getProfile(userName, password);
            String finalJSON = new Gson().toJson(result);
            resp.setContentType("application/json");
            resp.setCharacterEncoding("UTF-8");
            resp.getWriter().print(finalJSON);
        } else {
            resp.getWriter().print(0);
        }
    } else if (req.getParameter("loged").equals("yes")) {

        userName = req.getParameter("username");
        password = req.getParameter("password");

        if (CheckData.comfirmData(userName, password)) {
            start = req.getParameter("start");
            stop = req.getParameter("stop");
            resp.getWriter().print("Mam zgloszenie o " + start + " -> " + stop);
        } else {
            resp.getWriter().print(0);
        }
    } else {
        resp.getWriter().print(0);
    }
}

检查器:

    private static String localLogin = "Dragy";
private static String localPassword = "qwe";
    protected static boolean comfirmData(String userName, String password) {
    if (userName.equals(localLogin) && password.equals(localPassword)) {
        fillDataForUser();
        return true;
    } else {
        return false;
    }
}
于 2013-07-24T11:44:14.333 回答
1

在我看来,您正在向期望 GET 的服务器发送 POST 请求。除此之外,您的 POST 请求设置了 Content-Type 标头,但您实际上并未提供任何内容。

我预计这会导致请求失败,从而导致异常(您将忽略该异常)。这意味着从getJSONFromUrl返回的结果字符串为空,最终导致 JSON 解析器出现空指针异常。

另外,你为什么用一个空的BasicHttpParams对象覆盖默认的 HTTP 参数?如果您不使用该参数,客户端将使用默认的 HTTP 参数进行初始化,这似乎是一个更明智的选择。

您可以将 POST 请求转换为 GET 请求,并摆脱BasicHttpParams ,方法是替换getJSONFromUrl的前几行,如下所示:

 DefaultHttpClient   httpclient = new DefaultHttpClient();
 HttpGet httpget = new HttpGet(url);

 InputStream inputStream = null;
 String result = null;
 try {
     HttpResponse response = httpclient.execute(httpget);           
     HttpEntity entity = response.getEntity();

 ...

如果这不起作用,请在异常处理程序中添加一些日志记录以查看导致请求失败的原因。

于 2013-07-26T14:08:09.833 回答