繁体   English   中英

Android Web服务无法正常工作

[英]Android Web Services not working

我试图制作一个程序,将带有GCM注册ID的学生注册数据发送到服务器..当我尝试注册用户的父亲名字和母亲名字而没有姓氏时例如:Ajay

和例子

阿杰[空白] Sood

第一个示例中的名称可以在没有空格和姓氏的情况下正常工作。.但是,第二个示例中具有空格的名称则无法工作。

即使我在名字和姓氏之间使用下划线_也可以正常工作。 我不知道怎么了

RegistrationApp.java

package com.example.akshay.parentapp;

/**
 * Created by Akshay on 8/19/2015.
 */

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.gcm.GoogleCloudMessaging;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;


public class RegisterApp extends AsyncTask<Void, Void, String> {

    private static final String TAG = "GCMRelated";
    Context ctx;
    GoogleCloudMessaging gcm;
    String SENDER_ID = "237184799993";
    String regid = null;
    private int appVersion;
    String SNAME , CLASS , FNAME , MNAME , TIMINGS ;
    int ROLLNO ,  ROUTE;
    Long MNO , FNO;
    Double LAT , LONG;

    public RegisterApp(Context ctx, int rollno , String sname, GoogleCloudMessaging gcm, String classs , String fname,Long fno, String mname,Long mno,int route, String timings,Double lat , Double longg, int appVersion) {
        this.ctx = ctx;
        this.ROLLNO = rollno;
        this.SNAME = sname;
        this.CLASS = classs;
        this.FNAME = fname;
        this.FNO = fno;
        this.MNAME = mname;
        this.MNO = mno;
        this.ROUTE = route;
        this.TIMINGS = timings;
        this.LAT = lat;
        this.LONG = longg;

        this.gcm = gcm;
        this.appVersion = appVersion;

        Log.e("In Constructor", "-======================");

    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.e("In PreExecute", "-======================");
    }


    @Override
    protected String doInBackground(Void... arg0) {
        String msg = "";
        try {
            if (gcm == null) {
                gcm = GoogleCloudMessaging.getInstance(ctx);
            }
            regid = gcm.register(SENDER_ID);
            msg = "Device registered, registration ID=" + regid;

            // You should send the registration ID to your server over HTTP,
            // so it can use GCM/HTTP or CCS to send messages to your app.
            // The request to your server should be authenticated if your app
            // is using accounts.
            try {
                sendRegistrationIdToBackend();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }

            // For this demo: we don't need to send it because the device
            // will send upstream messages to a server that echo back the
            // message using the 'from' address in the message.

            // Persist the regID - no need to register again.
            storeRegistrationId(ctx, regid);
        } catch (IOException ex) {
            msg = "Error :" + ex.getMessage();
            // If there is an error, don't just keep trying to register.
            // Require the user to click a button again, or perform
            // exponential back-off.
        }
        Log.e("MESSAGE " , msg);
        Log.e("ROLLNO", String.valueOf(ROLLNO));
        return msg;

    }

    private void storeRegistrationId(Context ctx, String regid) {
        final SharedPreferences prefs = ctx.getSharedPreferences(MainActivity.class.getSimpleName(),
                Context.MODE_PRIVATE);
        Log.i(TAG, "Saving regId on app version " + appVersion);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("registration_id", regid);
        editor.putInt("appVersion", appVersion);
        editor.commit();
       Log.e("In Shared Prefs" , "-======================");

    }


    private void sendRegistrationIdToBackend() throws URISyntaxException {
       Log.e("INSEND REG TO BACK" , "HEY");
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI("http://akshay.site90.net/register.php?rollno="+ROLLNO+"&sname="+SNAME+"&regid="+regid+"&class="+CLASS+"&fname="+FNAME+"&fno="+FNO+"&mname="+MNAME+"&mno="+MNO+"&route="+ROUTE+"&timings="+TIMINGS +"&latitude="+LAT+"&longitude="+LONG));
        Log.e("RESULT", "http://akshay.site90.net/register.php?rollno=" + ROLLNO + "&sname=" + SNAME + "&regid=" + regid + "&class=" + CLASS + "&fname=" + FNAME + "&fno=" + FNO + "&mname=" + MNAME + "&mno=" + MNO + "&route=" + ROUTE + "&timings=" + TIMINGS + "&latitude=" + LAT + "&longitude=" + LONG);

        try {
            httpclient.execute(request);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("Something Went Wrong " , "ClientPro");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("Something Went Wrong ", "IOEX");
        }
    }


    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Toast.makeText(ctx, "Registration Completed. Now you can see the notifications", Toast.LENGTH_SHORT).show();
        Log.v(TAG, result);
        Toast.makeText(ctx , "In PostEx" , Toast.LENGTH_SHORT).show();

    }
}

姓和名之间带有_的logcat文件,例如firstname_lastname,效果很好:

18:31:51.632  29049-29049/? E/Zygote﹕ MountEmulatedStorage()
09-04 18:31:51.632  29049-29049/? E/Zygote﹕ v2
09-04 18:31:51.642  29049-29049/? E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
09-04 18:31:52.422  29049-29049/com.example.akshay.parentapp E/=====ROUTE====﹕ 401
09-04 18:32:35.082  29049-29049/com.example.akshay.parentapp E/ROLLNO==========﹕ 1
09-04 18:32:35.092  29049-29049/com.example.akshay.parentapp E/SNAME==========﹕ Akshay
09-04 18:32:35.092  29049-29049/com.example.akshay.parentapp E/REGID==========﹕ [ 09-04 18:32:35.092 29049:29049 E/CLASS========== ]
    Mca
09-04 18:32:35.092  29049-29049/com.example.akshay.parentapp E/FATHERNAME==========﹕ Ajay_Sood
09-04 18:32:35.092  29049-29049/com.example.akshay.parentapp E/FATHERNO==========﹕ 9876543210
09-04 18:32:35.092  29049-29049/com.example.akshay.parentapp E/MOTHERNAME==========﹕ Manju_Sood
09-04 18:32:35.092  29049-29049/com.example.akshay.parentapp E/MOTHERNO==========﹕ 9876543210
09-04 18:32:35.092  29049-29049/com.example.akshay.parentapp E/ROUTE==========﹕ 401
09-04 18:32:35.092  29049-29049/com.example.akshay.parentapp E/Timings==========﹕ 568
09-04 18:32:35.092  29049-29049/com.example.akshay.parentapp E/LAT==========﹕ 78.555454
09-04 18:32:35.092  29049-29049/com.example.akshay.parentapp E/LONG==========﹕ 78.545545
09-04 18:32:35.092  29049-29049/com.example.akshay.parentapp E/In Constructor﹕ -======================
09-04 18:32:35.092  29049-29049/com.example.akshay.parentapp E/In PreExecute﹕ -======================
09-04 18:32:35.332  29049-29128/com.example.akshay.parentapp E/INSEND REG TO BACK﹕ HEY
09-04 18:32:35.332  29049-29128/com.example.akshay.parentapp E/RESULT﹕ http://akshay.site90.net/register.php?rollno=1&sname=Akshay&regid=APA91bHeO4ugp3ENRU-XJb65Xjk99gB6s7r-7IF8680hgrn5RJugtG2xU5_IgSvk64wjYPvpczKIqeXlyrry7y2xT8Kj8CGF5kQnZ-68YeR4WGGEhc6d-dAZ6jsRUaCZ1G3_JMYfIMYJ&class=Mca&fname=Ajay_Sood&fno=9876543210&mname=Manju_Sood&mno=9876543210&route=401&timings=568&latitude=78.555454&longitude=78.545545
09-04 18:32:36.422  29049-29128/com.example.akshay.parentapp E/In Shared Prefs﹕ -======================
09-04 18:32:36.422  29049-29128/com.example.akshay.parentapp E/MESSAGE﹕ Device registered, registration ID=APA91bHeO4ugp3ENRU-XJb65Xjk99gB6s7r-7IF8680hgrn5RJugtG2xU5_IgSvk64wjYPvpczKIqeXlyrry7y2xT8Kj8CGF5kQnZ-68YeR4WGGEhc6d-dAZ6jsRUaCZ1G3_JMYfIMYJ
09-04 18:32:36.432  29049-29128/com.example.akshay.parentapp E/ROLLNO﹕ 1

尝试使用以下方法对值进行编码或对完整网址进行编码

    String query = URLEncoder.encode("first name", "utf-8");
    String url = "http://stackoverflow.com/search?q=" + query;

要么

    String encodedUrl = URLEncoder.encode(url, "UTF-8");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM