无法通过Android应用程序插入到azure中

时间:2013-04-30 09:31:00

标签: java android azure login

即时为我的应用程序执行登录注册,我必须将它们与microsoft azure集成在一起。但是,尽管遵循了microsoft azure给出的教程,我仍然无法将我的“字符串”插入其数据库中。代码中也没有错误,因此我不确定哪里出错了。以下是我的代码。

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("");           
        }
    });
}
}

1 个答案:

答案 0 :(得分:-2)

这是一段代码片段,希望它能为您提供帮助。

1)一个携带http get服务的函数

   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)扩展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)在onCreate方法中

     @Override
public void onCreate(Bundle savedInstanceState) {

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



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

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

   }

我提供的代码段以下列方式工作,

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

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

现在提供应该在客户端(Android)编写的代码,在服务器上接收该数据的后半部分

  • 服务器需要接收数据
  • 应该使用网络服务来做到这一点
  • 在服务器端实施Web服务
  • 只要android推送URL +数据
  • ,就会调用webservice
  • 获得数据后,根据需要对其进行操作