无法插入通过Android应用程序蔚蓝 [英] unable to insert into azure via android application

查看:162
本文介绍了无法插入通过Android应用程序蔚蓝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯做我的应用登录注册,我要他们微软Azure整合在一起。然而,尽管继微软Azure提供的教程后,我仍然无法插入我的串到他们的数据库。还有没有错误在codeS,所以我不是很确定哪里是错误的。下面是我的codeS。

 包mp.memberuse;进口java.net.MalformedURLException;进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.content.Shared preferences.Editor;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TabHost;
进口android.widget.TabHost.TabSpec;
进口android.widget.TextView;
进口com.microsoft.windowsazure.mobileservices *。公共类扩展登录登录注册{活动按钮BTN1,BTN2,btn3;
TF1的EditText,TF2,TF3,TF4,TF5,TF6,TF7,TF8,TF9,TF10,TF11;
TextView的TV1,TV2;私人MobileServiceClient mClient;公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    TabHost标签=(TabHost)this.findViewById(R.id.lt2tabhost);
    tabs.setup();    则tabspec TS1 = tabs.newTabSpec(登录);
    ts1.setIndicator(登录);
    ts1.setContent(R.id.c1);
    tabs.addTab(TS1);    则tabspec TS2 = tabs.newTabSpec(注册);
    ts2.setIndicator(注册);
    ts2.setContent(R.id.c2);
    tabs.addTab(TS2);    BTN1 =(按钮)findViewById(R.id.button1);
    BTN2 =(按钮)findViewById(R.id.button2);
    btn3 =(按钮)findViewById(R.id.button3);    TF1 =(EditText上)findViewById(R.id.editText1);
    TF2 =(EditText上)findViewById(R.id.editText2);
    TF3 =(EditText上)findViewById(R.id.editText3);
    TF4 =(EditText上)findViewById(R.id.editText4);
    TF5 =(EditText上)findViewById(R.id.editText5);
    TF6 =(EditText上)findViewById(R.id.editText6);
    TF7 =(EditText上)findViewById(R.id.editText7);
    TF8 =(EditText上)findViewById(R.id.editText8);
    TF9 =(EditText上)findViewById(R.id.editText9);
    TF10 =(EditText上)findViewById(R.id.editText10);    TV1 =(的TextView)findViewById(R.id.login);
    TV2 =(的TextView)findViewById(R.id.register);    尝试{
        MobileServiceClient mClient =新MobileServiceClient(https://testrun.azure-mobile.net/,FOJanABDYiJEVMHkCECAylrXJCnVwF77,这一点);
    }赶上(MalformedURLException的E){
        // TODO自动生成catch块
        e.printStackTrace();
    }    btn1.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v)
        {
            字符串的用户名,密码;
            。用户名= tf1.getText()的toString();
            。密码= tf2.getText()的toString();
                    /**if(username.equals(sqlusername)及&放大器; password.equals(SQLPASSWORD))
                    {
                        共享preferences preFS = getShared preferences(我的preferences,Context.MODE_PRIVATE);
                        共享preferences.Editor编辑器= prefs.edit();
                        editor.putString(全名,全名);
                        editor.commit();                        意向意图=新意图(LoginRegister.this,SendMessage.class);
                        startActivity(意向);
                    }
                    其他
                    {
                        tv1.setText(无效的用户);
                    } ** /
                }        }
    );    btn2.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v)
        {
            字符串的用户名,密码,cpassword,全名,身份证号码,地址,电话,电子邮件;
            。用户名= tf3.getText()的toString();
            。密码= tf4.getText()的toString();
            cpassword = tf5.getText()的toString()。
            全名= tf6.getText()的toString()。
            。身份证= tf7.getText()的toString();
            。地址= tf8.getText()的toString();
            。手机= tf9.getText()的toString();
            。电子邮件= tf10.getText()的toString();            会员会员=新成员();
            members.username =用户名;
            members.password =密码;
            members.fullname =全名;
            members.nric =身份证;
            members.address =地址;
            members.phone =电话;
            members.email =电子邮件;            如果(!password.equals(cpassword))
            {
                tv2.setText(密码&安培;确认密码不匹配。);
            }
            否则如果(username.equals()|| password.equals()|| cpassword.equals()|| fullname.equals()|| nric.equals()||地址。等于()|| phone.equals()|| email.equals())
            {
                tv2.setText(不要留下任何字段为空);
            }
            其他
            {
                mClient.getTable(Members.class).insert(成员,新TableOperationCallback<成员>()
                {
                    公共无效onCompleted(会员单位,异常例外,ServiceFilterResponse响应)
                    {
                        如果(例外== NULL)
                        {
                            tv2.setText(注册完成。);
                            tf3.setText();
                            tf4.setText();
                            tf5.setText();
                            tf6.setText();
                            tf7.setText();
                            tf8.setText();
                            tf9.setText();
                            tf10.setText();
                        }
                        其他
                        {
                            tv2.setText(注册失败!);
                        }
                    }
                });                    tv2.setText(注册完成。);
                    tf3.setText();
                    tf4.setText();
                    tf5.setText();
                    tf6.setText();
                    tf7.setText();
                    tf8.setText();
                    tf9.setText();
                    tf10.setText();
            }
        }
    });    btn3.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v)
        {
            tf3.setText();
            tf4.setText();
            tf5.setText();
            tf6.setText();
            tf7.setText();
            tf8.setText();
            tf9.setText();
            tf10.setText();
        }
    });
}
}


解决方案

下面是一个code片段,希望它会帮助你。

1),它携带的HTTP GET服务的功能。

 私人字符串SendDataFromAndroidDevice(){
    字符串结果=;    尝试{
        HttpClient的HttpClient的=新DefaultHttpClient();
        HTTPGET getMethod =新HTTPGET(您的网址+数据追加);        在的BufferedReader = NULL;
        BasicHtt presponse HTT presponse =(BasicHtt presponse)的HttpClient
                .execute(getMethod);
        在=新的BufferedReader(新的InputStreamReader(HTT presponse
                。.getEntity()的getContent()));        StringBuffer的SB =新的StringBuffer();
        串线=;
        而((行= in.readLine())!= NULL){
            sb.append(线);
        }
        附寄();
        结果= sb.toString();
    }赶上(例外五){
        e.printStackTrace();
    }
    返回结果;
}

2)延伸AsyncTask的一个类

 私有类HTTPdemo扩展
        AsyncTask的<弦乐,太虚,字符串> {    @覆盖
    在preExecute保护无效(){}    @覆盖
    保护字符串doInBackground(字符串... PARAMS){
        字符串结果= SendDataFromAndroidDevice();
        返回结果;
    }    @覆盖
    保护无效onProgressUpdate(虚空......值){}    @覆盖
    保护无效onPostExecute(字符串结果){        如果(结果=空&安培;!&安培;!result.equals()){
            尝试{
                JSONObject的resObject =新的JSONObject(结果);                }赶上(例外五){
                e.printStackTrace();
            }
        }
    }
}

3)内的onCreate方法

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);
    的setContentView(布局);    如果(选中这里,其中网络/互联网是avaliable){        新HTTPdemo()执行();
    }   }

由我提供的code段工程通过以下方式,

1)的 Android设备的URL +数据发送到服务器

2)服务器[使用Azure平台的]接收数据,并给出一个确认

现在应该在客户端(Android版)写的code提供给您,在服务器接收数据的后面部分是


  • 服务器需要接收数据

  • 一个WebService应被用来做

  • 实施在服务器端的web服务

  • web服务将被调用每当Android将推动URL +数据

  • 一旦你的数据,操纵它,只要你想

im doing a login register for my application and i have to integrate them together with microsoft azure. However, despite after following the tutorial given by microsoft azure, i still fail to insert my "string" into their database. There are also no error in the codes, hence i'm not very sure where is wrong. Below is my codes.

package mp.memberuse;

import java.net.MalformedURLException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import com.microsoft.windowsazure.mobileservices.*;

public class LoginRegister extends Activity {

Button btn1, btn2, btn3;
EditText tf1, tf2, tf3, tf4, tf5, tf6, tf7, tf8, tf9, tf10, tf11;
TextView tv1, tv2;

private MobileServiceClient mClient;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost tabs = (TabHost) this.findViewById(R.id.lt2tabhost);
    tabs.setup();

    TabSpec ts1 = tabs.newTabSpec("Login");
    ts1.setIndicator("Login");
    ts1.setContent(R.id.c1);
    tabs.addTab(ts1);

    TabSpec ts2 = tabs.newTabSpec("Register");
    ts2.setIndicator("Register");
    ts2.setContent(R.id.c2);
    tabs.addTab(ts2);

    btn1 = (Button)findViewById(R.id.button1);
    btn2 = (Button)findViewById(R.id.button2);
    btn3 = (Button)findViewById(R.id.button3);

    tf1=(EditText) findViewById(R.id.editText1);
    tf2=(EditText) findViewById(R.id.editText2);
    tf3=(EditText) findViewById(R.id.editText3);
    tf4=(EditText) findViewById(R.id.editText4);
    tf5=(EditText) findViewById(R.id.editText5);
    tf6=(EditText) findViewById(R.id.editText6);
    tf7=(EditText) findViewById(R.id.editText7);
    tf8=(EditText) findViewById(R.id.editText8);
    tf9=(EditText) findViewById(R.id.editText9);
    tf10=(EditText) findViewById(R.id.editText10);

    tv1=(TextView) findViewById(R.id.login);
    tv2=(TextView) findViewById(R.id.register);

    try {
        MobileServiceClient mClient = new MobileServiceClient("https://testrun.azure-mobile.net/","FOJanABDYiJEVMHkCECAylrXJCnVwF77",this);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    btn1.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            String username, password;
            username = tf1.getText().toString();
            password = tf2.getText().toString();


                    /**if(username.equals(sqlusername) && password.equals(sqlpassword))
                    {
                        SharedPreferences prefs = getSharedPreferences("myPreferences",Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = prefs.edit();
                        editor.putString("fullname", fullname);
                        editor.commit();

                        Intent intent = new Intent(LoginRegister.this, SendMessage.class);
                        startActivity(intent);
                    }
                    else
                    {
                        tv1.setText("Invalid user");
                    } **/
                }

        }
    );

    btn2.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            String username, password, cpassword, fullname, nric, address, phone, email;
            username = tf3.getText().toString();
            password = tf4.getText().toString();
            cpassword = tf5.getText().toString();
            fullname = tf6.getText().toString();
            nric = tf7.getText().toString();
            address = tf8.getText().toString();
            phone = tf9.getText().toString();
            email = tf10.getText().toString();

            Members members = new Members();
            members.username = username;
            members.password = password;
            members.fullname = fullname;
            members.nric = nric;
            members.address = address;
            members.phone = phone;
            members.email = email;

            if(!password.equals(cpassword))
            {
                tv2.setText("Password & Confirm Password does not match.");
            }
            else if(username.equals("") || password.equals("") || cpassword.equals("") || fullname.equals("") || nric.equals("") || address.equals("") || phone.equals("") || email.equals(""))
            {
                tv2.setText("Do not leave any field empty.");
            }
            else
            {
                mClient.getTable(Members.class).insert(members, new TableOperationCallback<Members>() 
                {
                    public void onCompleted(Members entity, Exception exception, ServiceFilterResponse response) 
                    {
                        if (exception == null)
                        {
                            tv2.setText("Register Complete.");
                            tf3.setText("");
                            tf4.setText("");
                            tf5.setText("");
                            tf6.setText("");
                            tf7.setText("");
                            tf8.setText("");
                            tf9.setText("");
                            tf10.setText(""); 
                        } 
                        else 
                        {
                            tv2.setText("Fail to register!");
                        }
                    }
                });

                    tv2.setText("Register Complete.");
                    tf3.setText("");
                    tf4.setText("");
                    tf5.setText("");
                    tf6.setText("");
                    tf7.setText("");
                    tf8.setText("");
                    tf9.setText("");
                    tf10.setText(""); 
            }
        }
    });

    btn3.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            tf3.setText("");
            tf4.setText("");
            tf5.setText("");
            tf6.setText("");
            tf7.setText("");
            tf8.setText("");
            tf9.setText("");
            tf10.setText("");           
        }
    });
}
}

解决方案

Here is an code snippet , hoping it will help you.

1)An function which carries the http get service

   private String SendDataFromAndroidDevice() {
    String result = "";

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet getMethod = new HttpGet("your url + data appended");

        BufferedReader in = null;
        BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient
                .execute(getMethod);
        in = new BufferedReader(new InputStreamReader(httpResponse
                .getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        while ((line = in.readLine()) != null) {
            sb.append(line);
        }
        in.close();
        result = sb.toString(); 


    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

2) An Class which extends AsyncTask

   private class HTTPdemo extends
        AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {}

    @Override
    protected String doInBackground(String... params) {
        String result = SendDataFromAndroidDevice();
        return result;
    }

    @Override
    protected void onProgressUpdate(Void... values) {}

    @Override
    protected void onPostExecute(String result) {

        if (result != null && !result.equals("")) {
            try {
                JSONObject resObject = new JSONObject(result);

                } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

3) Inside your onCreate method

     @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView("your layout");



    if ("check here where network/internet is avaliable") {

        new HTTPdemo().execute("");
    }

   }

The code snippet provided by me works in the following way,

1)Android device send the URL+data to server

2)Server [Azure platform used] receive the data and gives an acknowledgement

Now the Code which should be written at client side (Android) is provided to you, the later part of receiving that data at server is

  • Server needs to receive the data
  • An webservice should be used to do that
  • Implement an webservice at server side
  • The webservice will be invoked whenever android will push the URL+data
  • Once you have the data ,manipulated it as you want

这篇关于无法插入通过Android应用程序蔚蓝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆