2017-01-09 12 views
-2

私は、さらに移動する前に電子メールの確認を必要とするアンドロイドアプリをやっています。私はユーザーが自分のメールアドレスを入力してボタンを続行する簡単なedittextフィールドを持っています。バックエンドで私はあなたの電子メールが検証された電子メールのデータベースで利用可能な場合、ユーザーは次の活動に進むことができます。この.FOR私は私のAndroidアプリJavaクラスサーバーからの応答を返す方法

package fragment; 


import android.app.Activity; 
import android.content.Intent; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.example.user.timothysurvey.R; 
import com.example.user.timothysurvey.activity.Introduction; 

import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

import pojo.Email; 
import pojo.JSONParser; 

/** 
* A simple {@link Fragment} subclass. 
*/ 
public class Ongoing extends Fragment { 


    Button proceed; 
    EditText email; 
    TextView surveyTitle; 
    String success; 

    private static final String url="http://192.168.0.123/survey/public/api/verifyemail"; 

    public Ongoing() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v=inflater.inflate(R.layout.fragment_ongoing, container, false); 
     surveyTitle= (TextView) v.findViewById(R.id.surveyTitle); 
     email = (EditText) v.findViewById(R.id.email); 
     proceed= (Button) v.findViewById(R.id.proceed); 
     proceed.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(isConnected()) { 

        String emailAddress = email.getText().toString().trim(); 
        String emailPattern = "[a-zA-Z0-9._-][email protected][a-z]+\\.+[a-z]+"; 

        if (emailAddress.matches("")) { 

         Toast.makeText(getContext(), "Please fill up all the fields", Toast.LENGTH_LONG).show(); 

        } else { 
         if (emailAddress.matches(emailPattern)) { 
          new HttpAsyncTask().execute("http://192.168.0.123/survey/public/api/verifyemail"); 
         } else 
          Toast.makeText(getContext(), "Invalid Email Address", Toast.LENGTH_LONG).show(); 
        } 
       } 
      else { 

       Toast.makeText(getContext(), "Please check your internet connection", Toast.LENGTH_LONG).show(); 
      } 
      } 
     }); 
     return v; 
    } 

    private class HttpAsyncTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... urls) { 

      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("email", email.getText().toString())); 
      JSONParser jParser = new JSONParser(); 
      JSONObject json = jParser.makeHttpRequest(url, "POST", params); 
      try { 
       success = json.getString("success"); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
     @Override 
     protected void onPostExecute(String result) { 
      if(success.equals("Email verified. Please proceed with survey")) 
       Toast.makeText(getContext(), "Wait For a moment", Toast.LENGTH_LONG).show(); 
     } 
    } 

    public boolean isConnected(){ 
     ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Activity.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
     if (networkInfo != null && networkInfo.isConnected()) 
      return true; 
     else 
      return false; 
    } 

} 

をでこれを行なったし、これは、バックエンド

01のための私のPHPコードであります

Button proceed; 
EditText email; 
TextView surveyTitle; 
String success; 

更新HttpAsyncTask:私はcode.Somebody上から任意の結果を得ていないのです

は私がグローバル文字列変数を定義し、事前

答えて

0

で私target.Thankあなたを達成できる正しい方法を提案して下さいこのように:

private class HttpAsyncTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... urls) { 

     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("email", email.getText().toString())); 
     JSONParser jParser = new JSONParser(); 
     JSONObject json = jParser.makeHttpRequest(url, "POST", params); 
     try { 
      success = json.getString("success"); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(String result) { 
     if(success.equals("Email verified. Please proceed with survey")) 
      Toast.makeText(getContext(), "Wait For a moment", Toast.LENGTH_LONG).show(); 
    } 
} 

そして最後に、このようJSONParserクラスを作成します

public class JSONParser { 

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

public JSONParser() { 

} 

public JSONObject getJSONFromUrl(final String url) { 

    try { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 

     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 

     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    return jObj; 

} 

public JSONObject makeHttpRequest(String url, String method, 
     List<NameValuePair> params) { 

    try { 

     if (method == "POST") { 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     } else if (method == "GET") { 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      String paramString = URLEncodedUtils.format(params, "utf-8"); 
      url += "?" + paramString; 
      HttpGet httpGet = new HttpGet(url); 

      HttpResponse httpResponse = httpClient.execute(httpGet); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 
     } 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    return jObj; 
} } 
+0

あなたのコードで疲れました。この行にnullポインタ例外がスローされていますnullオブジェクト参照で仮想メソッド 'boolean java.lang.String.equals(java.lang.Object)'を呼び出そうとしています フラグメント.Ongoing $ HttpAsyncTask.onPostExecute(Ongoing.java:114) –

関連する問題