android无法将数据发布到服务器

时间:2014-11-06 09:00:55

标签: android

嘿,当我向服务器发布数据时,我遇到了这种错误,我不知道错误是什么,以及如何处理 这个错误只有在我使用移动数据而且不会在wifi或高速互联网上出现时才会出现

11-06 14:22:09.723: E/AndroidRuntime(18026): FATAL EXCEPTION: IntentService[Empty Constructor]
11-06 14:22:09.723: E/AndroidRuntime(18026): Process: com.androidhive.pushnotifications, PID: 18026
11-06 14:22:09.723: E/AndroidRuntime(18026): java.lang.NullPointerException
11-06 14:22:09.723: E/AndroidRuntime(18026):    at com.androidhive.pushnotifications.CapitalService.downloadUrl(CapitalService.java:123)
11-06 14:22:09.723: E/AndroidRuntime(18026):    at com.androidhive.pushnotifications.CapitalService.onHandleIntent(CapitalService.java:53)
11-06 14:22:09.723: E/AndroidRuntime(18026):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
11-06 14:22:09.723: E/AndroidRuntime(18026):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-06 14:22:09.723: E/AndroidRuntime(18026):    at android.os.Looper.loop(Looper.java:136)
11-06 14:22:09.723: E/AndroidRuntime(18026):    at android.os.HandlerThread.run(HandlerThread.java:61)

我的Intentservice代码

 protected void onHandleIntent(Intent intent) {


    subject = intent.getStringExtra("subject");
    length = intent.getStringExtra("length");
    reciever = intent.getStringExtra("reciever");
    sender = intent.getStringExtra("sender");
    Allmex = intent.getStringExtra("Allmex");

    url = "http://";

    try {
        // Retreiving capital from the url
        capital = downloadUrl(url);         

        // Creating an intent for broadcastreceiver
        Intent broadcastIntent = new Intent(Constants.BROADCAST_ACTION);

        // Attaching data to the intent
        broadcastIntent.putExtra(Constants.EXTRA_CAPITAL,Allmex);
        broadcastIntent.putExtra("subject", subject);
        broadcastIntent.putExtra("check_no1",length);
        // Sending the broadcast 
        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(broadcastIntent);  

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

/** A method to download data from url */
private String downloadUrl(String strUrl) throws IOException{
    String data = "";
    InputStream iStream = null;
    HttpURLConnection urlConnection = null;
    try{
            URL url = new URL(strUrl);


            // Creating an http connection to communicate with url 
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setReadTimeout(10000);
            urlConnection.setConnectTimeout(15000);
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("sender",sender));
            params.add(new BasicNameValuePair("reciever",reciever));
            params.add(new BasicNameValuePair("message",Allmex));
            params.add(new BasicNameValuePair("subject",subject));
            params.add(new BasicNameValuePair("id_no",Integer.parseInt(length)-1+""));
            OutputStream os = urlConnection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(getQuery(params));
            writer.flush();
            writer.close();
            os.close();

            // Connecting to url 
            urlConnection.connect();

            // Reading data from url 
            iStream = urlConnection.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

            StringBuffer sb  = new StringBuffer();

            String line = "";
            while( ( line = br.readLine())  != null){
                    sb.append(line);
            }

            data = sb.toString();               

            br.close();

    }catch(Exception e){
            Log.d("Exception while fetching data", e.toString());
    }finally{
         iStream.close();
         urlConnection.disconnect();
    }
    return data;
}   
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
    StringBuilder result = new StringBuilder();
    boolean first = true;

    for (NameValuePair pair : params)
    {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
    }

    return result.toString();
}

0 个答案:

没有答案